Assigning one object to another

  • Thread starter Thread starter Ricky W. Hunt
  • Start date Start date
R

Ricky W. Hunt

I came across something by accident; assigning one object to another (as in
object1 = object2) allows me to "work on" object2 just like it was object1.
For instance, in order to be able to reference a textbox on form1 I created
a textbox at module level called PassTextBox and assigned the original
textbox to it (textbox1 = PassTextBox) in Form1's load code. I was planning
on just using this as a "hold" area and when I got back into Form1 I could
assign PassTextBox back to textbox1. But I found out that whatever I did to
PassTextBox (such as changing the .text property) WAS ALREADY DONE IN
textbox1! So this makes me think there's something inherent in the object
that I can't see (or am not aware of) that contains an address (pointer).
Are the two textboxes two separate entities that just "point" to the same
place? I see this could open up a lot of possibilities but also a lot of
potential problems. How do you make a true "copy" of an object (so that
changing a property in one doesn't affect the other)? It's almost like the
assign is working ByRef instead of ByVal.
 
you are ending up with 2 names for the same object
remember - object variables are reference types

to avoid this (if you need to)
create a different object and just copy the values into the new object -
being careful not to make the same mistake on member objects of the object
you're copying -
or - look up the clone method
 
Hal Rosser said:
you are ending up with 2 names for the same object
remember - object variables are reference types

That's kind of what I had deduced. Thanks for claryfing. So if you had 100
such objects they'd really only take up the space of one?
to avoid this (if you need to)
create a different object and just copy the values into the new object -
being careful not to make the same mistake on member objects of the object
you're copying -
or - look up the clone method

thanks.
 
Ricky W. Hunt said:
That's kind of what I had deduced. Thanks for claryfing. So if you had 100
such objects they'd really only take up the space of one?

Yep. Technically, the space of one and the space of [up to] 100 pointers to
it. In general, "reference type" means "variable is only a pointer". A
side effect of this is that ByRef and ByVal are pretty much the same for
objects and/or reference types. ByVal will still make a copy, as you would
expect, but it is only copying the pointer, not the entire object.

Some more explanation:

http://msdn.microsoft.com/library/d.../vbcn7/html/vaconargumentpassingmechanism.asp

Best Regards,

Andy
 
Andy Becker said:
Ricky W. Hunt said:
That's kind of what I had deduced. Thanks for claryfing. So if you had 100
such objects they'd really only take up the space of one?

Yep. Technically, the space of one and the space of [up to] 100 pointers to
it. In general, "reference type" means "variable is only a pointer". A
side effect of this is that ByRef and ByVal are pretty much the same for
objects and/or reference types. ByVal will still make a copy, as you would
expect, but it is only copying the pointer, not the entire object.

Wow. This is kind of shocking but good to know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top