Results 1 to 4 of 4

Thread: Create a byref variable?

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    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?

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    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 .
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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
  •  



Click Here to Expand Forum to Full Width