Click to See Complete Forum and Search --> : Passing Classes as arguments to other Classes


deepak_warrier
October 13th, 1999, 06:18 AM
I am not able to pass classes to other classes.

In Class1, put the code



public A as Byte





In Form1, put the code




private Sub Form_Load()

Dim X as new Class1
Dim Coll as new Collection

X.A=25

Coll.Add X

'BookMark1

X.A=6

Coll.Add X

End Sub





When I make "X.A=6", the value of the first item in the collection also becomes "6". So, at BookMark1, I added the statement "Set X=Nothing". Is that necessary, or have I made a mistake somewhere? I have the same problem whenever I pass Objects to another Object.

Deepak

Lothar Haensler
October 13th, 1999, 06:24 AM
you have been using the same reference. Of course, the value changed in X.
code like this:

dim x as class1
set x = new class1
x.a = 25
col1.add x
set x = new class1
x.a = 6
col1.add x

deepak_warrier
October 13th, 1999, 07:04 AM
Suppose I have a sub like



private Sub A( byval B as Class1)

'Code Here
'More Code

End Sub




Do I have to "Set B=Nothing" before I end the Sub? Or will this be done automatically?

Deepak

Lothar Haensler
October 13th, 1999, 07:51 AM
IMHO you should not do it

deepak_warrier
October 13th, 1999, 12:08 PM
What's IMHO?

Lothar Haensler
October 14th, 1999, 01:21 AM
common acronym in Email:
In my humble opinion

Bruno
October 14th, 1999, 07:21 AM
I hope you understand difference between passing arguments ByVal and ByRef.
(If you pass variable ByVal, your sub cannot change original value of variable.)

For OBJECT variables, behaviour is different.
If you pass OBJECT variable ByVal, you CAN modify original Object properties, but you cannot modify its address (i.e. you cannot Set it to another object or destroy it).
See: http://www.vb2themax.com/Item.asp?PageID=MistakeBank&ID=11

Since you are passing your object ByVal: Sub A(byval B as Class1) I thought you should have this info.