473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ref variables in a class

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
15 1032
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
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
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
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
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
"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

"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
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
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
"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
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
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
"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

"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
Sorry for forgetting about the line breaks.....
Nov 21 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
17
by: Davíđ Ţórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
6
by: darrel | last post by:
I'm still not quite sure how best to handle the passing of data between controls. This is a method I'm using at the moment: I have an XML file that contains a variety of page-centric...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
4
by: Nick Dreyer | last post by:
Is it possible to see public class variables of a COM addin in Excel 97 VBA? I have successfully created the (Visual Basic 2003 .NET) COM and referenced it in an Excel 97 VBA project. The VBA...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.