PC Review


Reply
Thread Tools Rate Thread

Assigning one object to another

 
 
Ricky W. Hunt
Guest
Posts: n/a
 
      10th Aug 2004
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.

--
Thanks,
Ricky W. Hunt
freendeed


 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      11th Aug 2004
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



"Ricky W. Hunt" <(E-Mail Removed)> wrote in message
news:wZbSc.282915$XM6.153027@attbi_s53...
> 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.
>
> --
> Thanks,
> Ricky W. Hunt
> freendeed
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/30/2004


 
Reply With Quote
 
Ricky W. Hunt
Guest
Posts: n/a
 
      11th Aug 2004
"Hal Rosser" <(E-Mail Removed)> wrote in message
news:%dhSc.4813$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Andy Becker
Guest
Posts: n/a
 
      11th Aug 2004
"Ricky W. Hunt" <(E-Mail Removed)> wrote in message
news:bTiSc.284516$Oq2.99946@attbi_s52...
> 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/de...gmechanism.asp

Best Regards,

Andy


 
Reply With Quote
 
Ricky W. Hunt
Guest
Posts: n/a
 
      11th Aug 2004
"Andy Becker" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Ricky W. Hunt" <(E-Mail Removed)> wrote in message
> news:bTiSc.284516$Oq2.99946@attbi_s52...
> > 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning properties Values from one object to other parez Microsoft C# .NET 5 18th Mar 2008 10:46 PM
Assigning null to the locked object? Ympostor Microsoft C# .NET 1 5th Nov 2007 11:19 AM
Assigning an array to range object? =?Utf-8?B?U21pdGNo?= Microsoft Excel Programming 2 16th Feb 2007 03:10 PM
Assigning an Object to a variable MAC Microsoft Access External Data 4 6th Feb 2004 11:03 PM
Assigning session object within thread William Mild Microsoft VB .NET 6 2nd Oct 2003 09:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:02 PM.