Reference vs Value

  • Thread starter Thread starter derian
  • Start date Start date
D

derian

This question seems almost too trivial and i feel i should know the answer
but alas...

I have a class called 'RandomObject'

I have an instance of the class...
dim Blah as new RandomObject.

I set a few properties on the class and so on and so forth.
Now i create a new reference to the class...
dim TemporaryBlah as new RandomObject

I set the temporary value to the initial value...
TemporaryBlah = Blah

Make some changes to Blah and then would like to return to my initial state
by setting blah to the temporary value...
Blah = TemporaryBlah

Of course since Blah and TemporaryBlah are references to a class, a
reference type, they actually do not contain the data but instead a
reference to the data so they are in fact the same thing... with all that
being said, how can i keep temporaryBlah static when i make the changes to
blah? I would basically like to treat the reference type like a value
type... is that possible?

:\\derian
 
how can i keep temporaryBlah static when i make the changes to
blah? I would basically like to treat the reference type like a value
type... is that possible?

You need to clone the object using the .clone/.copy methods (if available)
otherwise I think you need to implement the ICloneable Interface.
 
If it's your object that you created, (I do this a lot) I create a second
constructor (New) that takes a reference to its own type. Then I peel off
the values from the type into the new object.

You can also serialize to a memory stream if your object is serializable and
the deserialize to the new object.
 
it is my own object... sorry for not specifying!
i'll give that a shot and see what happens... thanks for the input guys.
 
Back
Top