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

ref variables in a class

P: n/a
z f
Hi,

i use VB.NET and defined a class that should hold a ref member variables to
a instance of the same class type.

if i define the variable of the class type and then assigns a instance/value
to this member, will it point to the instance i assigned it to or to a copy
of the data?

TIA,z.
Nov 21 '05 #1
Share this Question
Share on Google+
15 Replies

P: n/a
Z F,

Either if you use byvalue or byref than only the address of the object will
be passed.

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
Cor, do you mean that if I have a reference object and pass it byVal to a
function then a complete copy of the data is made in memory..for example

public class myclasstest
.....
end class

in my main program,
dim b as new myclasstest

Call myFunctiontest(byval b)

Is a complete copy made of b in memory?
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Z F,

Either if you use byvalue or byref than only the address of the object will
be passed.

I hope this helps,

Cor

Nov 21 '05 #3

P: n/a
no, a copy of the pointer is made in memory....
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Cor, do you mean that if I have a reference object and pass it byVal to a
function then a complete copy of the data is made in memory..for example

public class myclasstest
....
end class

in my main program,
dim b as new myclasstest

Call myFunctiontest(byval b)

Is a complete copy made of b in memory?
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Z F,

Either if you use byvalue or byref than only the address of the object
will
be passed.

I hope this helps,

Cor

Nov 21 '05 #4

P: n/a
Then what happens if I change a property of b in myFunctionTest..the original
b in the calling code wasn't changed since b was passed byVal but the b in
myFunction will contain the changed property. Not sure if I understand how
this works? Any clarification would really be appreciated.
--
Dennis in Houston
"Rick Mogstad" wrote:
no, a copy of the pointer is made in memory....
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Cor, do you mean that if I have a reference object and pass it byVal to a
function then a complete copy of the data is made in memory..for example

public class myclasstest
....
end class

in my main program,
dim b as new myclasstest

Call myFunctiontest(byval b)

Is a complete copy made of b in memory?
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Z F,

Either if you use byvalue or byref than only the address of the object
will
be passed.

I hope this helps,

Cor


Nov 21 '05 #5

P: n/a
Dennis,

Does running this sample give you an idea?

\\\
Public Class Main
Public Shared Sub main()
Dim aa As String = "I am an original value"
Dim bb As New Dennis
bb.bb = "I am an original value in an object"
a(aa) 'the value is passed
MessageBox.Show(aa.ToString)
a(bb.bb) 'the value in the object is passed
MessageBox.Show(bb.bb.ToString)
a(bb) 'the object is passed
MessageBox.Show(bb.bb.ToString)
End Sub
Private Shared Sub a(ByVal aa As String)
aa = "My value is changed"
End Sub
Private Shared Sub a(ByVal aa As Dennis)
aa.bb = "My value is changed in the original object"
End Sub
End Class
Public Class Dennis
Public bb As String
End Class
///

I hope this helps,

Cor
Nov 21 '05 #6

P: n/a
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
Then what happens if I change a property of b in myFunctionTest..the
original b in the calling code wasn't changed since b was passed byVal
but the b in myFunction will contain the changed property. public class myclasstest
end class

dim b as new myclasstest

Call myFunctiontest(byval b)


This last statement doesn't compile; at least not in my copy of VB'2003.
You're not calling some strange API function, here, are you?

Regards,
Phill W.
Nov 21 '05 #7

P: n/a

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
Then what happens if I change a property of b in myFunctionTest..the
original
b in the calling code wasn't changed since b was passed byVal but the b in
myFunction will contain the changed property. Not sure if I understand
how
this works? Any clarification would really be appreciated.
--
Dennis in Houston

The property will be changed in both the b in your original class, and b in
the called function
Nov 21 '05 #8

P: n/a
Then when a reference object is passed byVal, you can still change it in the
function that you called...so what's the difference between passing a
reference object byVal or byRef...you can change the object no matter how you
pass it!
--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Dennis,

Does running this sample give you an idea?

\\\
Public Class Main
Public Shared Sub main()
Dim aa As String = "I am an original value"
Dim bb As New Dennis
bb.bb = "I am an original value in an object"
a(aa) 'the value is passed
MessageBox.Show(aa.ToString)
a(bb.bb) 'the value in the object is passed
MessageBox.Show(bb.bb.ToString)
a(bb) 'the object is passed
MessageBox.Show(bb.bb.ToString)
End Sub
Private Shared Sub a(ByVal aa As String)
aa = "My value is changed"
End Sub
Private Shared Sub a(ByVal aa As Dennis)
aa.bb = "My value is changed in the original object"
End Sub
End Class
Public Class Dennis
Public bb As String
End Class
///

I hope this helps,

Cor

Nov 21 '05 #9

P: n/a
I tried it with Dennis as a structure and found that the original structure
wasn't changed. I guess original Value types are not changed when passed
byVal but Reference types are. This doesn't seem very consistent!

--
Dennis in Houston
"Cor Ligthert [MVP]" wrote:
Dennis,

Does running this sample give you an idea?

\\\
Public Class Main
Public Shared Sub main()
Dim aa As String = "I am an original value"
Dim bb As New Dennis
bb.bb = "I am an original value in an object"
a(aa) 'the value is passed
MessageBox.Show(aa.ToString)
a(bb.bb) 'the value in the object is passed
MessageBox.Show(bb.bb.ToString)
a(bb) 'the object is passed
MessageBox.Show(bb.bb.ToString)
End Sub
Private Shared Sub a(ByVal aa As String)
aa = "My value is changed"
End Sub
Private Shared Sub a(ByVal aa As Dennis)
aa.bb = "My value is changed in the original object"
End Sub
End Class
Public Class Dennis
Public bb As String
End Class
///

I hope this helps,

Cor

Nov 21 '05 #10

P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb
Then when a reference object is passed byVal, you can still change
it in the function that you called...so what's the difference
between passing a reference object byVal or byRef...you can change
the object no matter how you pass it!

Right, but if you pass it ByRef, you have a reference to the original
reference and you can modify the original reference. If you pass it ByVal,
the original reference can not be modified.
Example:

Shared Sub main()
Dim s As String = "before"
RefTypeByVal(s)
Debug.WriteLine(s) 'still "before"
RefTypeByRef(s)
Debug.WriteLine(s) 'now "new string"
End Sub
Shared Sub RefTypeByVal(ByVal s As String)
s = "new string"
End Sub
Shared Sub RefTypeByRef(ByRef s As String)
s = "new string"
End Sub


Actually it is very simple:

ByVal: A copy of the value is passed.
ByRef: A reference to the value is passed.

The value can either be an object or a reference to an object.

That's all you have to remember.

Armin

Nov 21 '05 #11

P: n/a
Thanks..I think I got it now. If I pass s as byVal, I can change the object
that s points to but I cannot change s's pointer. If I pass s as byRef, then
I can set s to a new object and it will be returned to the calling routine.
Thanks again.
--
Dennis in Houston
"Armin Zingler" wrote:
"Dennis" <De****@discussions.microsoft.com> schrieb
Then when a reference object is passed byVal, you can still change
it in the function that you called...so what's the difference
between passing a reference object byVal or byRef...you can change
the object no matter how you pass it!

Right, but if you pass it ByRef, you have a reference to the original
reference and you can modify the original reference. If you pass it ByVal,
the original reference can not be modified.
Example:

Shared Sub main()
Dim s As String = "before"
RefTypeByVal(s)
Debug.WriteLine(s) 'still "before"
RefTypeByRef(s)
Debug.WriteLine(s) 'now "new string"
End Sub
Shared Sub RefTypeByVal(ByVal s As String)
s = "new string"
End Sub
Shared Sub RefTypeByRef(ByRef s As String)
s = "new string"
End Sub


Actually it is very simple:

ByVal: A copy of the value is passed.
ByRef: A reference to the value is passed.

The value can either be an object or a reference to an object.

That's all you have to remember.

Armin

Nov 21 '05 #12

P: n/a
Dennis,
I tried it with Dennis as a structure and found that the original structure
wasn't changed. I guess original Value types are not changed when passed
byVal but Reference types are. This doesn't seem very consistent!

However that is exactly as it is.

:-)

Cor
Nov 21 '05 #13

P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb
Thanks..I think I got it now. If I pass s as byVal, I can change
the object that s points to but I cannot change s's pointer. If I
pass s as byRef, then I can set s to a new object and it will be
returned to the calling routine. Thanks again.

Exact. :-)

Armin
Nov 21 '05 #14

P: n/a

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
I tried it with Dennis as a structure and found that the original structure
wasn't changed. I guess original Value types are not changed when passed
byVal but Reference types are. This doesn't seem very consistent!

But it makes sense

A reference type stores a pointer to your object.
ByRef passes a pointer to that pointer, allowing you to
change the original. pointer->original pointer->Object
ByVal passes a copy of the pointer, so any changes to object
are saved,
reference changes are not
copy of pointer->Object

A value type actually stores the data at the location
ByRef passes a pointer to the value. pointer->Value
ByVal passes a copy of the value. Value

Nov 21 '05 #15

P: n/a
Sorry for forgetting about the line breaks.....
Nov 21 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.