|
-
Jul 16th, 2009, 12:15 AM
#1
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
#2
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
|