Niklas,
Why would you need to redesign your app?
ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable. Property Set routines should not be modifying the caller's
variable!
A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.
Dim x As Person
x = New Person()
Dim y As Person
y = x
Both x & y are the exact same Person object on the heap.
A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.
Dim x As Integer
x = 100
Dim y As Integer
y = x
Although both x & y have the value 100, they are physically different values
as a copy was made.
Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.
When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.
Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.
Hope this helps
Jay
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
Thank you. That means that I have to redesign my application because the
Set part of Property do not allow ByRef only ByVal...maybe I use a field. Regards
/Niklas
"Shiva" wrote:
Hi,
The output you are getting is indeed correct.
The way ByVal & ByRef differs in case of reference type is like this:
When a ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of
the class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original
var was pointing at.
I have modified the code below to show the difference. Run it and check
the results.
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com...
Hi
What I have learned is that a variable is just a reference when dealing
with Objects.
Are you supposed to use ByVal or ByRef in functions? They produce the
same result or have I missed something?
Regards
/Niklas
Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
Console.WriteLine("Org: testPropObj.MyInt = " & testPropObj.MyInt)
ChangeObjectByVal(testPropObj)
Console.WriteLine("ByVal: testPropObj = " & testPropObj.MyInt)
ChangeObjectByRef(testPropObj)
Console.WriteLine("ByRef: testPropObj = " & testPropObj.MyInt)
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub
Public Shared Sub ChangeObjectByVal(ByVal myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub
Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class
Public Class MyPropertObject
Public MyInt As Integer
End Class