Hi, All,

How to declare the class:
Class A
Property A1
Property A2

For each property, there are two properties B1 and B2, so you can use then
as following:
dim cls: set cls = new A
cls.A1.B1="11"
cls.A1.B2="12"
cls.A2.B1="21"
cls.A2.B2="22"

Another question is:
if the Class A has 20 properties and assign each one to a value with array:
cls.A1=ar(1)
cls.A2=ar(2)
cls.A3=ar(3)
...

How to use a loop for it?

Thanks,
-Andrew

Re: Basic Class Concept by Anthony

Anthony
Thu Nov 29 14:09:23 PST 2007

"Andrew HUANG" <AndrewHUANG@discussions.microsoft.com> wrote in message
news:F9AB4668-36B9-4DD3-9B5A-00C8C84BEC0B@microsoft.com...
> Hi, All,
>
> How to declare the class:
> Class A
> Property A1
> Property A2
>
> For each property, there are two properties B1 and B2, so you can use then
> as following:
> dim cls: set cls = new A
> cls.A1.B1="11"
> cls.A1.B2="12"
> cls.A2.B1="21"
> cls.A2.B2="22"
>
> Another question is:
> if the Class A has 20 properties and assign each one to a value with
array:
> cls.A1=ar(1)
> cls.A2=ar(2)
> cls.A3=ar(3)
> ...
>
> How to use a loop for it?
>

You haven't provided enough info to answer this correctly.

Is the number of properties fixed to 20 or is it more arbitary?
Are the propertied really index by number or do each have a meaning that
would better be expressed using a name?

Here is a good place to start to learn about Class in VBScript:-

http://msdn2.microsoft.com/en-us/library/4ah5852c.aspx





--
Anthony Jones - MVP ASP/ASP.NET



Re: Basic Class Concept by mayayana

mayayana
Thu Nov 29 16:09:23 PST 2007

The whole point of a class is to hide the details
and just provide a clearly defined, usable object
to the outside. cls.A1.B1 implies that you have
another A1 class that can be created via the
cls.A1 property. If A1 has two properties it's
better to use an array or named parameter. How you
do that is up to you - whatever seems the most
intuitive. For instance, with your array sample,
you could do something like:

Dim s, cls
Set cls = new C1

cls.A(0) = "ok"
MsgBox cls.A(0)

Set cls = Nothing

Class C1
Private array1(9)

Public Property Let A(index, value)
If index > -1 And index < 10 Then
array1(index) = value
End If
End Property

Public Property Get A(index)
If index > -1 And index < 10 Then
A = array1(index)
End If
End Property

End Class

In that class the property A is a group of
10 values, but from outside the class the
array is not visible.

Design is really the big part of writing a class.
You might end up re-writing it more than once until
you find the right interface that seems intuitive
to you.



> How to declare the class:
> Class A
> Property A1
> Property A2
>
> For each property, there are two properties B1 and B2, so you can use then
> as following:
> dim cls: set cls = new A
> cls.A1.B1="11"
> cls.A1.B2="12"
> cls.A2.B1="21"
> cls.A2.B2="22"
>
> Another question is:
> if the Class A has 20 properties and assign each one to a value with
array:
> cls.A1=ar(1)
> cls.A2=ar(2)
> cls.A3=ar(3)
> ...
>
> How to use a loop for it?
>
> Thanks,
> -Andrew
>



RE: Basic Class Concept by AndrewHUANG

AndrewHUANG
Fri Nov 30 06:22:02 PST 2007

Thanks, Anthony and Mayayana.

The first part is about nested class. Because each property in Class A has
other two properties B1 and B2.

The second part, assume 10 properties in class A and must use meaningful name.

Regards,
-Andrew

"Andrew HUANG" wrote:

> Hi, All,
>
> How to declare the class:
> Class A
> Property A1
> Property A2
>
> For each property, there are two properties B1 and B2, so you can use then
> as following:
> dim cls: set cls = new A
> cls.A1.B1="11"
> cls.A1.B2="12"
> cls.A2.B1="21"
> cls.A2.B2="22"
>
> Another question is:
> if the Class A has 20 properties and assign each one to a value with array:
> cls.A1=ar(1)
> cls.A2=ar(2)
> cls.A3=ar(3)
> ...
>
> How to use a loop for it?
>
> Thanks,
> -Andrew
>

Re: Basic Class Concept by mayayana

mayayana
Fri Nov 30 07:06:20 PST 2007



> The first part is about nested class. Because each property in Class A has
> other two properties B1 and B2.
>

Yes, you can do it, but it doesn't seem to make sense.
You're turning almost every variable into a class. Classes
are not meant for just holding variables. (You could use
a string array for that.) They're meant for encapsulating
complex functionality in order to simplify your code and
simplify repetitive operations. You should be thinking
about how you want that functionality presented, and
then figure out how to do it. In other words, figure out
the design of the class from the outside - the methods
and properties - before figuring out the logistics.

If you really do have the design all figured out then
you need to write another class for A. That's somewhat
awkward in script because, unlike compiled software, you'll
need to have the code for that class cluttering your
script. The more inter-related classes you create, the more
involved it gets maintaining it. But if that's what you want
to do, you'd write a Class A with two properties. If you want
another class to hold two instances of A then you need
to also write a CLASS C, say, that holds instances of A:

Set C1 = new C
Set A1 = C1.GetMeAnAFirst
A1.B1 = 11
Set A2 = C1.GetMeAnASecond
A2.B1 = 21

See how convoluted that's getting already?



Re: Basic Class Concept by Anthony

Anthony
Sat Dec 01 13:16:10 PST 2007

"Andrew HUANG" <AndrewHUANG@discussions.microsoft.com> wrote in message
news:5613F35E-E42E-49CB-BB5E-F5D452A2FF55@microsoft.com...
>
> "Andrew HUANG" wrote:
>
> > Hi, All,
> >
> > How to declare the class:
> > Class A
> > Property A1
> > Property A2
> >
> > For each property, there are two properties B1 and B2, so you can use
then
> > as following:
> > dim cls: set cls = new A
> > cls.A1.B1="11"
> > cls.A1.B2="12"
> > cls.A2.B1="21"
> > cls.A2.B2="22"
> >
> > Another question is:
> > if the Class A has 20 properties and assign each one to a value with
array:
> > cls.A1=ar(1)
> > cls.A2=ar(2)
> > cls.A3=ar(3)
> > ...
> >
> > How to use a loop for it?
> >
> > Thanks,
> > -Andrew
> >
> Thanks, Anthony and Mayayana.
>
> The first part is about nested class. Because each property in Class A has
> other two properties B1 and B2.
>

Class A

Private moB1
Private moB2

Private Sub Class_Initialize()
Set moB1 = New B
Set moB2 = New B
End Sub

Public Property Get B1()
Set B1 = moB1
End Property

Public Property Get B2()
Set B2 = moB2
End Property

End Class

Class B

Private msSomeValue
Private msOtherValue

Public Property Get SomeValue()
SomeValue = msSomeValue
End Property

Public Property Let SomeValue(ByVal vsValue)
msSomeValue= vsValue
End Property

Public Property Get OtherValue()
OtherValue= msOtherValue
End Property

Public Property Let OtherValue(ByVal vsValue)
msOtherValue= vsValue
End Property

End Class

Dim oA : Set oA = New A

oA.B1.SomeValue = "Hello"
oA.B1.OtherValue = "World"
oA.B2.SomeValue = "Yasso"
oA.B2.OtherValue = "Kosmos"


> The second part, assume 10 properties in class A and must use meaningful
name.
>

Nope. Not in any way that is better than simply assigning each value in the
array to its appropriate named property. After all the relationship between
an array index and the named property needs to be described somewhere.


--
Anthony Jones - MVP ASP/ASP.NET