By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

Stupid question about object reference

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <st***********@hotmail.com> wrote in message
news:eb**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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" <st***********@hotmail.com>
| 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: <eb**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 205.141.32.1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.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
|
|
|

Nov 20 '05 #3

P: n/a
* "STom" <st***********@hotmail.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.