variable assignment vs reference

alishahak

New member
Joined
May 19, 2006
Messages
1
Programming Experience
Beginner
I am quite new to VB.net programming. I am currently dealing with a problem which I hope someone can help me solve...

In my code, I have created a class that includes several public variables, like integers and strings, etc. I assign different values to them throughout the code. However, I don't seem to be able to create a second copy of this object variable by simply using the equal sign, i.e.:

ClassVariable2 = ClassVariable1

It appears to me that the above operation creates a reference to ClassVariable1, since changing it will also change ClassVariable2. What am I missing in my code? I have also made sure that I instantiate ClassVariable2 with New keyword before assignment...

any suggestions will be highly appreaciated.

A.
 
When you do object to object assignment like that... yes, it'll simply set a reference to the first. To get a copy, you have to instanciate the second instance, then copy the properties over one by one. OR... if it's a class that you have control over, you can implement a Copy method that returns a new instance of the class (it will also have to copy the properties, but you would only need to do it once.)

-tg
 
or make a constructor that initializes all the parameters (if they are needed for operation of the class then the constructor should do this anyway. the constructor is intended that no instance of a class be possible whereby it is useless after construction because further parameters need setting) and then make a new class with the constructor. if you want to use parameters of class 1 then then is possible. do bear in mind that any parameters that are not primitive types will be passed by reference:


Dim myClass2 as Class2 = New Class2(myClass1.StringArg, myClass1.IntArg)

'here we now have a myClass2 with its own int but sharing a reference to the same string


in this case it doesnt matter much that they share a string because strings are immutable. every operation on a string that returns a modified string, returns a new instance of another string somewhere else in memory


in some cases the two objects would be sharing a mutable object (suppose they were sharing a stringbuilder) so if you said:

Dim myClass2 as Class2 = New Class2(myClass1.StringBuilderArg)

myClass1.StringBuilderParameter.Clear()
myClass1.StringBuilderParameter.Append("hello")

MsgBox myClass2.StringBuilderParameter.ToString() 'prints hello



most of the time this doesnt matter (and its the best way to do things), but jsut be aware of it. If there is something that really needs to be a separate copy of something, entirely, then you need to deep copy it - i.e. all its parameters and all their parameters etc etc need to be cloned.. I cant really think of a time ive needed to do this in 9 years of programming
 
i forgot to attacha pic to help visualize my first point:
 

Attachments

  • untitled.GIF
    untitled.GIF
    3.6 KB · Views: 41
Back
Top