Stupid question about object reference

  • Thread starter Thread starter STom
  • Start date Start date
S

STom

Ok, if I have a class:

Public Class MyXClass
Public Property X as Integer
.....
End Class

In my code if I have two instances of this class:

Dim myX1 as new MyXClass
Dim myX2 as MyXClass

myX2 = myX1

Is myX2 a reference to the myX1 object or is it just a copy? In other words
if I modify the property within myX2, will it change myX1.

I believe it is a by reference situation but I could not find anything in
the MSDN documentation to confirm this. I"m sure its there, just don't know
exactly where.

STom
 
There is no copy created, both variables point to the same instance of the
object. Creating a copy woudl bring up the question of whether or not it
should be a deep copy, if so, then you could be copying large sized objects,
etc.

Structures however, are copied. So if in your example these weren't
classes, but structures, there would be 2 instance of the structure, with
each variable having its own copy.
 
STom,

Your assumption is correct, in your code example myX1 and myX2 both point
to the same MyXClass object. Changes in myX1 can be seen in myX2; they both
reference the same object.


hope that helps

Steve Stein
VB Team

This posting is provided "AS IS" with no warranties and confers no rights.


--------------------
| From: "STom" <[email protected]>
| Subject: Stupid question about object reference
| Date: Mon, 19 Jan 2004 14:21:36 -0500
| Lines: 24
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 205.141.32.1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:174211
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Ok, if I have a class:
|
| Public Class MyXClass
| Public Property X as Integer
| ....
| End Class
|
| In my code if I have two instances of this class:
|
| Dim myX1 as new MyXClass
| Dim myX2 as MyXClass
|
| myX2 = myX1
|
| Is myX2 a reference to the myX1 object or is it just a copy? In other
words
| if I modify the property within myX2, will it change myX1.
|
| I believe it is a by reference situation but I could not find anything in
| the MSDN documentation to confirm this. I"m sure its there, just don't
know
| exactly where.
|
| STom
|
|
|
 
* "STom said:
Ok, if I have a class:

Public Class MyXClass
Public Property X as Integer
....
End Class

In my code if I have two instances of this class:

Dim myX1 as new MyXClass
Dim myX2 as MyXClass

myX2 = myX1

Is myX2 a reference to the myX1 object or is it just a copy? In other words
if I modify the property within myX2, will it change myX1.

I believe it is a by reference situation but I could not find anything in
the MSDN documentation to confirm this.

Keywords: Value types, Reference types.
 
Back
Top