|
-
Jul 16th, 2009, 12:03 AM
#1
Thread Starter
Lively Member
Create a byref variable?
How would I create a variable and assign it to another variable and when I change one variable it will be changing the other also?
-
Jul 16th, 2009, 12:15 AM
#2
Fanatic Member
Re: Create a byref variable?
You should read carefully this section :
Code:
Module Module1
' ByVal
'Updated: August 2008
'Specifies that an argument is passed in such a way that the called
'procedure or property cannot change the value of a variable underlying the argument in the calling code.
' The following example demonstrates the use of the ByVal parameter passing mechanism with a reference
'type argument. In the example, the argument is c1, an instance of class Class1.
'ByVal prevents the code in the procedures from changing the underlying value
'of the reference argument, c1, but does not protect the accessible fields and properties of c1.
Sub Main()
' Declare an instance of the class and assign a value to its field.
Dim c1 As Class1 = New Class1()
c1.Field = 5
Console.WriteLine("Original value for the field: " & c1.Field)
' ByVal does not prevent changing the value of a field or property.
ChangeFieldValue(c1)
Console.WriteLine("Value of field after ChangeFieldValue: " & c1.Field)
' ByVal does prevent changing the value of c1 itself.
ChangeFieldValue2(c1)
Console.WriteLine("Value of field after ChangeFieldValue: " & c1.Field)
ChangeClassReference(c1)
Console.WriteLine("Value of field after ChangeClassReference: " & c1.Field)
End Sub
Public Sub ChangeFieldValue(ByVal cls As Class1)
cls.Field = 500
End Sub
Public Sub ChangeFieldValue2(ByVal cls As Class1)
cls.Field = 700
End Sub
Public Sub ChangeClassReference(ByVal cls As Class1)
cls = New Class1()
cls.Field = 1000
End Sub
Public Class Class1
Public Field As Integer
End Class
End Module
Byrel is vice versa .
-
Jul 16th, 2009, 12:15 AM
#3
Re: Create a byref variable?
There's no such thing as a ByRef variable. Only method parameters are declared ByRef or ByVal. When it comes to variables, their behaviour depends on their type. If the type of a variable is a reference type, which basically means a class, then it will behave as you describe. If it's type is a value type, which basically means a structure, then it won't. To see what I mean about reference types, try this code:
Code:
Dim list1 As New List(Of String)
Dim list2 As List(Of String) = list1
MessageBox.Show(list1.Count.ToString(), "list1")
MessageBox.Show(list2.Count.ToString(), "list2")
list1.Add("Hello World")
MessageBox.Show(list1.Count.ToString(), "list1")
MessageBox.Show(list2.Count.ToString(), "list2")
As you will see, adding an item to list1 also adds it to list2. That's not really accurate though. You don't actually add an item to list1 or list2, because they are just variables. You actually add an item to the List object referred to by the list1 variable. The list2 variable refers to the same List object so list2 reflects the change too.
As an analogy, let's say that your mother is wearing a red dress. That would mean that your father's wife is wearing a red dress too, right. Now, let's say that your father's wife changes into a blue dress. Has your mother's dress changed too? Absolutely. Your mother and your father's wife are the same person (assuming the standard nuclear family ). They are just two different ways to refer to the same thing.
Now, if you explain to us exactly what you're trying to achieve then we can probably suggest the best way to achieve it.
-
Jul 16th, 2009, 08:15 AM
#4
Re: Create a byref variable?
If you have a value type (e.g. an integer) you want to treat as a reference type, then you could wrapper it in a class, and pass that class object around. However, this only addresses your initial question, and there may be a better way of doing what you want.
Code:
Public Class Thing
Public Value as Integer
End Class
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|