byval vs byref

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boni

    byval vs byref

    Dear all,
    I found out that I don' understand byVal/byRef in VB.
    There is a simple example:
    Why in the first test the result is 10,10 where in the second 0,20.
    Thanks for your help.
    Boni
    Module Module1

    Class A

    Public B As Integer

    End Class

    Sub test(ByVal val_ As A, ByRef ref_ As A)

    val_.B = 10

    ref_.B = 10

    End Sub

    Sub test2(ByVal aVal As A, ByRef aRef As A)

    Dim A1 As New A

    Dim A2 As New A

    A1.B = 20

    A2.B = 20

    aVal = A1

    aRef = A2

    End Sub



    Sub Main()

    Dim A1 As A

    Dim A2 As A

    A1 = New A

    A2 = New A

    test(A1, A2)

    Console.WriteLi ne(A1.B)

    Console.WriteLi ne(A2.B)

    A1 = New A

    A2 = New A

    test2(A1, A2)

    Console.WriteLi ne(A1.B)

    Console.WriteLi ne(A2.B)

    End Sub

    End Module


  • Robin Tucker

    #2
    Re: byval vs byref

    Think about it like this:

    When you pass by reference you can create a new item with "New" and that new
    item will be visible back to the caller. When you pass by reference, you
    can change the state of the item, but you cannot assign a new one and have
    that visible back to the caller.

    "Boni" <oilia@nospam > wrote in message
    news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=blue]
    > Dear all,
    > I found out that I don' understand byVal/byRef in VB.
    > There is a simple example:
    > Why in the first test the result is 10,10 where in the second 0,20.
    > Thanks for your help.
    > Boni
    > Module Module1
    >
    > Class A
    >
    > Public B As Integer
    >
    > End Class
    >
    > Sub test(ByVal val_ As A, ByRef ref_ As A)
    >
    > val_.B = 10
    >
    > ref_.B = 10
    >
    > End Sub
    >
    > Sub test2(ByVal aVal As A, ByRef aRef As A)
    >
    > Dim A1 As New A
    >
    > Dim A2 As New A
    >
    > A1.B = 20
    >
    > A2.B = 20
    >
    > aVal = A1
    >
    > aRef = A2
    >
    > End Sub
    >
    >
    >
    > Sub Main()
    >
    > Dim A1 As A
    >
    > Dim A2 As A
    >
    > A1 = New A
    >
    > A2 = New A
    >
    > test(A1, A2)
    >
    > Console.WriteLi ne(A1.B)
    >
    > Console.WriteLi ne(A2.B)
    >
    > A1 = New A
    >
    > A2 = New A
    >
    > test2(A1, A2)
    >
    > Console.WriteLi ne(A1.B)
    >
    > Console.WriteLi ne(A2.B)
    >
    > End Sub
    >
    > End Module
    >
    >[/color]


    Comment

    • Ken Tucker [MVP]

      #3
      Re: byval vs byref

      Hi,

      Imagine you have a form that need to be filled out. If you give
      a copy of the form to someone to fill out the orginal doesnt change (byval).
      If you give them the orginal to fill out they hand it back to you changed
      (byref). Hope that helps.

      Ken
      ---------------
      "Boni" <oilia@nospam > wrote in message
      news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..
      Dear all,
      I found out that I don' understand byVal/byRef in VB.
      There is a simple example:
      Why in the first test the result is 10,10 where in the second 0,20.
      Thanks for your help.
      Boni
      Module Module1

      Class A

      Public B As Integer

      End Class

      Sub test(ByVal val_ As A, ByRef ref_ As A)

      val_.B = 10

      ref_.B = 10

      End Sub

      Sub test2(ByVal aVal As A, ByRef aRef As A)

      Dim A1 As New A

      Dim A2 As New A

      A1.B = 20

      A2.B = 20

      aVal = A1

      aRef = A2

      End Sub



      Sub Main()

      Dim A1 As A

      Dim A2 As A

      A1 = New A

      A2 = New A

      test(A1, A2)

      Console.WriteLi ne(A1.B)

      Console.WriteLi ne(A2.B)

      A1 = New A

      A2 = New A

      test2(A1, A2)

      Console.WriteLi ne(A1.B)

      Console.WriteLi ne(A2.B)

      End Sub

      End Module



      Comment

      • Robin Tucker

        #4
        Re: byval vs byref

        Well that was a load of bollocks (edited below!):
        [color=blue]
        >
        > When you pass by reference you can create a new item with "New" and that
        > new item will be visible back to the caller. When you pass by value, you
        > can change the state of the item, but you cannot assign a new one and have
        > that visible back to the caller.
        >[/color]


        Comment

        • Bob Powell [MVP]

          #5
          Re: byval vs byref

          When you pass by value you pass a copy of the original. The code that
          recieves the value can sometimes change it but when the subroutine is ended
          the value, including any changes is destroyed along with the other values
          that were in the scope of the method.

          When you pass by reference you pass the address of the original item of
          data. Any changes made in the subroutine are made to the original item of
          data via that reference and when the suboroutine ends it's only the
          refeference and not the data that is destroyed.

          As a design choice you would pass by value when you simply want to inform
          the subroutine of some parameter or another and not care about what the
          subroutine does with it. You would pass by reference if you want the
          subroutine to do some computation that permanently alters the data.

          --
          Bob Powell [MVP]
          Visual C#, System.Drawing

          Find great Windows Forms articles in Windows Forms Tips and Tricks


          Answer those GDI+ questions with the GDI+ FAQ


          All new articles provide code in C# and VB.NET.
          Subscribe to the RSS feeds provided and never miss a new article.





          "Boni" <oilia@nospam > wrote in message
          news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=blue]
          > Dear all,
          > I found out that I don' understand byVal/byRef in VB.
          > There is a simple example:
          > Why in the first test the result is 10,10 where in the second 0,20.
          > Thanks for your help.
          > Boni
          > Module Module1
          >
          > Class A
          >
          > Public B As Integer
          >
          > End Class
          >
          > Sub test(ByVal val_ As A, ByRef ref_ As A)
          >
          > val_.B = 10
          >
          > ref_.B = 10
          >
          > End Sub
          >
          > Sub test2(ByVal aVal As A, ByRef aRef As A)
          >
          > Dim A1 As New A
          >
          > Dim A2 As New A
          >
          > A1.B = 20
          >
          > A2.B = 20
          >
          > aVal = A1
          >
          > aRef = A2
          >
          > End Sub
          >
          >
          >
          > Sub Main()
          >
          > Dim A1 As A
          >
          > Dim A2 As A
          >
          > A1 = New A
          >
          > A2 = New A
          >
          > test(A1, A2)
          >
          > Console.WriteLi ne(A1.B)
          >
          > Console.WriteLi ne(A2.B)
          >
          > A1 = New A
          >
          > A2 = New A
          >
          > test2(A1, A2)
          >
          > Console.WriteLi ne(A1.B)
          >
          > Console.WriteLi ne(A2.B)
          >
          > End Sub
          >
          > End Module
          >
          >[/color]


          Comment

          • Chris

            #6
            Re: byval vs byref

            The easy way to think about it is that it is just the same as pointer to
            your object or var.


            "Boni" <oilia@nospam > wrote in message
            news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=blue]
            > Dear all,
            > I found out that I don' understand byVal/byRef in VB.
            > There is a simple example:
            > Why in the first test the result is 10,10 where in the second 0,20.
            > Thanks for your help.
            > Boni
            > Module Module1
            >
            > Class A
            >
            > Public B As Integer
            >
            > End Class
            >
            > Sub test(ByVal val_ As A, ByRef ref_ As A)
            >
            > val_.B = 10
            >
            > ref_.B = 10
            >
            > End Sub
            >
            > Sub test2(ByVal aVal As A, ByRef aRef As A)
            >
            > Dim A1 As New A
            >
            > Dim A2 As New A
            >
            > A1.B = 20
            >
            > A2.B = 20
            >
            > aVal = A1
            >
            > aRef = A2
            >
            > End Sub
            >
            >
            >
            > Sub Main()
            >
            > Dim A1 As A
            >
            > Dim A2 As A
            >
            > A1 = New A
            >
            > A2 = New A
            >
            > test(A1, A2)
            >
            > Console.WriteLi ne(A1.B)
            >
            > Console.WriteLi ne(A2.B)
            >
            > A1 = New A
            >
            > A2 = New A
            >
            > test2(A1, A2)
            >
            > Console.WriteLi ne(A1.B)
            >
            > Console.WriteLi ne(A2.B)
            >
            > End Sub
            >
            > End Module
            >
            >[/color]


            Comment

            • JohnFol

              #7
              Re: byval vs byref

              Robin / Ken / Bob / Chris, sorry but I read this differently. In Boni's 2 examples, both have the 2nd argument byRef.

              I was going down the line of Reference Types vs Value types.
              If you have MSDN locally installed, the following gives an almost identical example

              ms-help://MS.MSDNQTR.2005 JAN.1033/vbls7/html/vblrfVBSpec6_1. htm


              Failing that search under the "Visual Basic Language Specification" for
              7.1 Value Types and Reference Types


              "Boni" <oilia@nospam > wrote in message news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Dear all,
              > I found out that I don' understand byVal/byRef in VB.
              > There is a simple example:
              > Why in the first test the result is 10,10 where in the second 0,20.
              > Thanks for your help.
              > Boni
              > Module Module1
              >
              > Class A
              >
              > Public B As Integer
              >
              > End Class
              >
              > Sub test(ByVal val_ As A, ByRef ref_ As A)
              >
              > val_.B = 10
              >
              > ref_.B = 10
              >
              > End Sub
              >
              > Sub test2(ByVal aVal As A, ByRef aRef As A)
              >
              > Dim A1 As New A
              >
              > Dim A2 As New A
              >
              > A1.B = 20
              >
              > A2.B = 20
              >
              > aVal = A1
              >
              > aRef = A2
              >
              > End Sub
              >
              >
              >
              > Sub Main()
              >
              > Dim A1 As A
              >
              > Dim A2 As A
              >
              > A1 = New A
              >
              > A2 = New A
              >
              > test(A1, A2)
              >
              > Console.WriteLi ne(A1.B)
              >
              > Console.WriteLi ne(A2.B)
              >
              > A1 = New A
              >
              > A2 = New A
              >
              > test2(A1, A2)
              >
              > Console.WriteLi ne(A1.B)
              >
              > Console.WriteLi ne(A2.B)
              >
              > End Sub
              >
              > End Module
              >
              >[/color]

              Comment

              • Chris

                #8
                Re: byval vs byref

                byref is a point to an object you have already created
                byval is the object it self

                "Boni" <oilia@nospam > wrote in message
                news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                > Dear all,
                > I found out that I don' understand byVal/byRef in VB.
                > There is a simple example:
                > Why in the first test the result is 10,10 where in the second 0,20.
                > Thanks for your help.
                > Boni
                > Module Module1
                >
                > Class A
                >
                > Public B As Integer
                >
                > End Class
                >
                > Sub test(ByVal val_ As A, ByRef ref_ As A)
                >
                > val_.B = 10
                >
                > ref_.B = 10
                >
                > End Sub
                >
                > Sub test2(ByVal aVal As A, ByRef aRef As A)
                >
                > Dim A1 As New A
                >
                > Dim A2 As New A
                >
                > A1.B = 20
                >
                > A2.B = 20
                >
                > aVal = A1
                >
                > aRef = A2
                >
                > End Sub
                >
                >
                >
                > Sub Main()
                >
                > Dim A1 As A
                >
                > Dim A2 As A
                >
                > A1 = New A
                >
                > A2 = New A
                >
                > test(A1, A2)
                >
                > Console.WriteLi ne(A1.B)
                >
                > Console.WriteLi ne(A2.B)
                >
                > A1 = New A
                >
                > A2 = New A
                >
                > test2(A1, A2)
                >
                > Console.WriteLi ne(A1.B)
                >
                > Console.WriteLi ne(A2.B)
                >
                > End Sub
                >
                > End Module
                >
                >[/color]


                Comment

                • JP

                  #9
                  Re: byval vs byref

                  If you pass a variable ByVal, the procedure cannot modify the variable
                  itself. But, if the argument is a reference type, you can modify the members
                  of the object to which it points, even though you cannot replace the object
                  itself.

                  If you pass a variable ByRef, the procedure can modify the variable itself.
                  If the argument is an object variable, you can assign a new object to it as
                  well.

                  Like in

                  Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                  System.EventArg s) Handles Button1.Click
                  Dim a As Integer, b As Integer
                  a = 10
                  b = 20
                  ValRef(a, b)
                  MsgBox("a=" & a & " ; b=" & b) 'Return a=10 ; b=25
                  ValRefTextBox(T extBox1, TextBox2) 'Set TextBox1.Text=1 0 and
                  TextBox2.Text=2 0
                  End Sub

                  Private Sub ValRef(ByVal a, ByRef b)
                  a += 5
                  b += 5
                  End Sub

                  Private Sub ValRefTextBox(B yVal t1 As TextBox, ByRef t2 As TextBox)
                  t1.Text = 10
                  t2.Text = 20
                  End Sub

                  Cheers,
                  JP
                  ------------------------------------------------------------------
                  A program is a device used to convert,
                  data into error messages
                  ------------------------------------------------------------------
                  "Chris" <ccalzaretta@ho tmail.com> wrote in message
                  news:umufNY%23r FHA.2876@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                  > byref is a point to an object you have already created
                  > byval is the object it self
                  >
                  > "Boni" <oilia@nospam > wrote in message
                  > news:%23uY6m5cZ FHA.3908@TK2MSF TNGP15.phx.gbl. ..[color=green]
                  >> Dear all,
                  >> I found out that I don' understand byVal/byRef in VB.
                  >> There is a simple example:
                  >> Why in the first test the result is 10,10 where in the second 0,20.
                  >> Thanks for your help.
                  >> Boni
                  >> Module Module1
                  >>
                  >> Class A
                  >>
                  >> Public B As Integer
                  >>
                  >> End Class
                  >>
                  >> Sub test(ByVal val_ As A, ByRef ref_ As A)
                  >>
                  >> val_.B = 10
                  >>
                  >> ref_.B = 10
                  >>
                  >> End Sub
                  >>
                  >> Sub test2(ByVal aVal As A, ByRef aRef As A)
                  >>
                  >> Dim A1 As New A
                  >>
                  >> Dim A2 As New A
                  >>
                  >> A1.B = 20
                  >>
                  >> A2.B = 20
                  >>
                  >> aVal = A1
                  >>
                  >> aRef = A2
                  >>
                  >> End Sub
                  >>
                  >>
                  >>
                  >> Sub Main()
                  >>
                  >> Dim A1 As A
                  >>
                  >> Dim A2 As A
                  >>
                  >> A1 = New A
                  >>
                  >> A2 = New A
                  >>
                  >> test(A1, A2)
                  >>
                  >> Console.WriteLi ne(A1.B)
                  >>
                  >> Console.WriteLi ne(A2.B)
                  >>
                  >> A1 = New A
                  >>
                  >> A2 = New A
                  >>
                  >> test2(A1, A2)
                  >>
                  >> Console.WriteLi ne(A1.B)
                  >>
                  >> Console.WriteLi ne(A2.B)
                  >>
                  >> End Sub
                  >>
                  >> End Module
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  Working...