Differance between ByVal and BeRef when handling objects

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

    Differance between ByVal and BeRef when handling objects

    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.MyI nt = 1
    Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
    ChangeObjectByV al(testPropObj)
    Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
    ChangeObjectByR ef(testPropObj)
    Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
    Console.WriteLi ne("Press Enter to exit...")
    Console.ReadLin e()
    End Sub

    Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
    myObject.MyInt = 5
    End Sub

    Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
    myObject.MyInt = 6
    End Sub
    End Class

    Public Class MyPropertObject
    Public MyInt As Integer
    End Class

  • Shiva

    #2
    Re: Differance between ByVal and BeRef when handling objects

    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" <Niklas@discuss ions.microsoft. com> wrote in message
    news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.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.MyI nt = 1
    Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
    ChangeObjectByV al(testPropObj)
    Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
    ChangeObjectByR ef(testPropObj)
    Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
    Console.WriteLi ne("Press Enter to exit...")
    Console.ReadLin e()
    End Sub

    Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
    myObject = New MyPropertObject
    myObject.MyInt = 5
    End Sub

    Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
    myObject = New MyPropertObject
    myObject.MyInt = 6
    End Sub
    End Class

    Public Class MyPropertObject
    Public MyInt As Integer
    End Class


    Comment

    • Cor Ligthert

      #3
      Re: Differance between ByVal and BeRef when handling objects

      Hi Niklas,

      You can almost forever use ByVal
      It is with a value the value itself and with an existing object the value of
      the reference of the object.

      Above I write already when it cannot be used ByVal, that is when the object
      is declared however not created yet. Than you have to do it for an object
      ByRef.

      I hope this helps?

      Cor


      Comment

      • Niklas

        #4
        Re: Differance between ByVal and BeRef when handling objects

        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:
        [color=blue]
        > 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" <Niklas@discuss ions.microsoft. com> wrote in message
        > news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.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.MyI nt = 1
        > Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
        > ChangeObjectByV al(testPropObj)
        > Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
        > ChangeObjectByR ef(testPropObj)
        > Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
        > Console.WriteLi ne("Press Enter to exit...")
        > Console.ReadLin e()
        > End Sub
        >
        > Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
        > myObject = New MyPropertObject
        > myObject.MyInt = 5
        > End Sub
        >
        > Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
        > myObject = New MyPropertObject
        > myObject.MyInt = 6
        > End Sub
        > End Class
        >
        > Public Class MyPropertObject
        > Public MyInt As Integer
        > End Class
        >
        >
        >[/color]

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: Differance between ByVal and BeRef when handling objects

          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" <Niklas@discuss ions.microsoft. com> wrote in message
          news:9BEB80B7-A5C0-4596-B262-6642EBAF285E@mi crosoft.com...[color=blue]
          > Thank you. That means that I have to redesign my application because the[/color]
          Set part of Property do not allow ByRef only ByVal...maybe I use a field.[color=blue]
          > Regards
          > /Niklas
          >
          > "Shiva" wrote:
          >[color=green]
          > > Hi,
          > >
          > > The output you are getting is indeed correct.
          > >
          > > The way ByVal & ByRef differs in case of reference type is like this:[/color][/color]
          When a[color=blue][color=green]
          > > 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[/color][/color]
          the[color=blue][color=green]
          > > 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[/color][/color]
          var[color=blue][color=green]
          > > was pointing at.
          > >
          > > I have modified the code below to show the difference. Run it and check[/color][/color]
          the[color=blue][color=green]
          > > results.
          > >
          > > "Niklas" <Niklas@discuss ions.microsoft. com> wrote in message
          > > news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.com...
          > > Hi
          > > What I have learned is that a variable is just a reference when dealing[/color][/color]
          with[color=blue][color=green]
          > > Objects.
          > > Are you supposed to use ByVal or ByRef in functions? They produce the[/color][/color]
          same[color=blue][color=green]
          > > result or have I missed something?
          > > Regards
          > > /Niklas
          > >
          > > Public Class Main
          > > Shared Sub Main()
          > > Dim testPropObj As New MyPropertObject
          > > testPropObj.MyI nt = 1
          > > Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
          > > ChangeObjectByV al(testPropObj)
          > > Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
          > > ChangeObjectByR ef(testPropObj)
          > > Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
          > > Console.WriteLi ne("Press Enter to exit...")
          > > Console.ReadLin e()
          > > End Sub
          > >
          > > Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
          > > myObject = New MyPropertObject
          > > myObject.MyInt = 5
          > > End Sub
          > >
          > > Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
          > > myObject = New MyPropertObject
          > > myObject.MyInt = 6
          > > End Sub
          > > End Class
          > >
          > > Public Class MyPropertObject
          > > Public MyInt As Integer
          > > End Class
          > >
          > >
          > >[/color][/color]


          Comment

          • David Anton

            #6
            re:Differance between ByVal and BeRef when handling objects

            I used to use this as an interview question...

            When you're passing an object to a method - you're passing a variable
            which points to the object. Both ByVal or ByRef pass a variable
            which points to the object since a copy of an address is still the
            valid address, so the object modifications persists upon leaving the
            method. All ByVal does in this case is prevent you from changing the
            variable pointing to the object to point to another object upon
            leaving the method.

            Comment

            • Dennis

              #7
              Re: Differance between ByVal and BeRef when handling objects

              Are you sure that when you pass an array ByVal that a copy is made of the entire array?
              --
              Dennis in Houston


              "Shiva" wrote:
              [color=blue]
              > 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" <Niklas@discuss ions.microsoft. com> wrote in message
              > news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.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.MyI nt = 1
              > Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
              > ChangeObjectByV al(testPropObj)
              > Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
              > ChangeObjectByR ef(testPropObj)
              > Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
              > Console.WriteLi ne("Press Enter to exit...")
              > Console.ReadLin e()
              > End Sub
              >
              > Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
              > myObject = New MyPropertObject
              > myObject.MyInt = 5
              > End Sub
              >
              > Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
              > myObject = New MyPropertObject
              > myObject.MyInt = 6
              > End Sub
              > End Class
              >
              > Public Class MyPropertObject
              > Public MyInt As Integer
              > End Class
              >
              >
              >[/color]

              Comment

              • Niklas

                #8
                Re: Differance between ByVal and BeRef when handling objects

                Thanks all of you. I did some test on my application and ByVal did the job.
                Regards
                /Niklas

                "Jay B. Harlow [MVP - Outlook]" wrote:
                [color=blue]
                > 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" <Niklas@discuss ions.microsoft. com> wrote in message
                > news:9BEB80B7-A5C0-4596-B262-6642EBAF285E@mi crosoft.com...[color=green]
                > > Thank you. That means that I have to redesign my application because the[/color]
                > Set part of Property do not allow ByRef only ByVal...maybe I use a field.[color=green]
                > > Regards
                > > /Niklas
                > >
                > > "Shiva" wrote:
                > >[color=darkred]
                > > > Hi,
                > > >
                > > > The output you are getting is indeed correct.
                > > >
                > > > The way ByVal & ByRef differs in case of reference type is like this:[/color][/color]
                > When a[color=green][color=darkred]
                > > > 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[/color][/color]
                > the[color=green][color=darkred]
                > > > 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[/color][/color]
                > var[color=green][color=darkred]
                > > > was pointing at.
                > > >
                > > > I have modified the code below to show the difference. Run it and check[/color][/color]
                > the[color=green][color=darkred]
                > > > results.
                > > >
                > > > "Niklas" <Niklas@discuss ions.microsoft. com> wrote in message
                > > > news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.com...
                > > > Hi
                > > > What I have learned is that a variable is just a reference when dealing[/color][/color]
                > with[color=green][color=darkred]
                > > > Objects.
                > > > Are you supposed to use ByVal or ByRef in functions? They produce the[/color][/color]
                > same[color=green][color=darkred]
                > > > result or have I missed something?
                > > > Regards
                > > > /Niklas
                > > >
                > > > Public Class Main
                > > > Shared Sub Main()
                > > > Dim testPropObj As New MyPropertObject
                > > > testPropObj.MyI nt = 1
                > > > Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
                > > > ChangeObjectByV al(testPropObj)
                > > > Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
                > > > ChangeObjectByR ef(testPropObj)
                > > > Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
                > > > Console.WriteLi ne("Press Enter to exit...")
                > > > Console.ReadLin e()
                > > > End Sub
                > > >
                > > > Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
                > > > myObject = New MyPropertObject
                > > > myObject.MyInt = 5
                > > > End Sub
                > > >
                > > > Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
                > > > myObject = New MyPropertObject
                > > > myObject.MyInt = 6
                > > > End Sub
                > > > End Class
                > > >
                > > > Public Class MyPropertObject
                > > > Public MyInt As Integer
                > > > End Class
                > > >
                > > >
                > > >[/color][/color]
                >
                >
                >[/color]

                Comment

                • Shiva

                  #9
                  Re: Differance between ByVal and BeRef when handling objects

                  Hi,

                  I meant a 'copy of the reference' (address) is made not what the ref points
                  to.

                  "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                  news:D8F20062-ECEB-44CD-88C1-1CB18C521C78@mi crosoft.com...
                  Are you sure that when you pass an array ByVal that a copy is made of the
                  entire array?
                  --
                  Dennis in Houston


                  "Shiva" wrote:
                  [color=blue]
                  > Hi,
                  >
                  > The output you are getting is indeed correct.
                  >
                  > The way ByVal & ByRef differs in case of reference type is like this: When[/color]
                  a[color=blue]
                  > 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[/color]
                  the[color=blue]
                  > 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[/color]
                  var[color=blue]
                  > was pointing at.
                  >
                  > I have modified the code below to show the difference. Run it and check[/color]
                  the[color=blue]
                  > results.
                  >
                  > "Niklas" <Niklas@discuss ions.microsoft. com> wrote in message
                  > news:1852F9EB-5D65-406B-90EC-7539BC63E4D5@mi crosoft.com...
                  > Hi
                  > What I have learned is that a variable is just a reference when dealing[/color]
                  with[color=blue]
                  > Objects.
                  > Are you supposed to use ByVal or ByRef in functions? They produce the[/color]
                  same[color=blue]
                  > result or have I missed something?
                  > Regards
                  > /Niklas
                  >
                  > Public Class Main
                  > Shared Sub Main()
                  > Dim testPropObj As New MyPropertObject
                  > testPropObj.MyI nt = 1
                  > Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
                  > ChangeObjectByV al(testPropObj)
                  > Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
                  > ChangeObjectByR ef(testPropObj)
                  > Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
                  > Console.WriteLi ne("Press Enter to exit...")
                  > Console.ReadLin e()
                  > End Sub
                  >
                  > Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
                  > myObject = New MyPropertObject
                  > myObject.MyInt = 5
                  > End Sub
                  >
                  > Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
                  > myObject = New MyPropertObject
                  > myObject.MyInt = 6
                  > End Sub
                  > End Class
                  >
                  > Public Class MyPropertObject
                  > Public MyInt As Integer
                  > End Class
                  >
                  >
                  >[/color]


                  Comment

                  • - HAL9000

                    #10
                    Re: Differance between ByVal and BeRef when handling objects

                    Yea I've wondered the same thing. Duplicating a large array sounds
                    prohibitively expensive. Maybe the byref and byvalue or just
                    specifying permission to write ????

                    Forrest

                    On Thu, 22 Jul 2004 19:00:03 -0700, Dennis
                    <Dennis@discuss ions.microsoft. com> wrote:
                    [color=blue]
                    >Are you sure that when you pass an array ByVal that a copy is made of the entire array?[/color]


                    Comment

                    • - HAL9000

                      #11
                      Re: Differance between ByVal and BeRef when handling objects

                      Should one be allowed to write to an array that is passed by value?

                      If you are then that doesn't sound consistent with passing an integer
                      byvalue. Doesn't passing byvalue imply that you absolutely don't want
                      the original integer to be modified?

                      Forrest

                      On 22 Jul 2004 17:03:34 -0500,
                      dave@tangibleso ftwaresolutions-dot-com.no-spam.invalid (David Anton)
                      wrote:
                      [color=blue]
                      >I used to use this as an interview question...
                      >
                      >When you're passing an object to a method - you're passing a variable
                      >which points to the object. Both ByVal or ByRef pass a variable
                      >which points to the object since a copy of an address is still the
                      >valid address, so the object modifications persists upon leaving the
                      >method. All ByVal does in this case is prevent you from changing the
                      >variable pointing to the object to point to another object upon
                      >leaving the method.[/color]


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: Differance between ByVal and BeRef when handling objects

                        Forrest,
                        See my earlier response. You are confusing passing parameters ByVal & ByRef
                        with Value Types & Reference types.

                        Hope this helps
                        Jay

                        "- HAL9000" <gumpy@mail.org > wrote in message
                        news:i8j3g0t8ao icotk621an0dbhd gtcvf68qh@4ax.c om...[color=blue]
                        > Should one be allowed to write to an array that is passed by value?
                        >
                        > If you are then that doesn't sound consistent with passing an integer
                        > byvalue. Doesn't passing byvalue imply that you absolutely don't want
                        > the original integer to be modified?
                        >
                        > Forrest
                        >
                        > On 22 Jul 2004 17:03:34 -0500,
                        > dave@tangibleso ftwaresolutions-dot-com.no-spam.invalid (David Anton)
                        > wrote:
                        >[color=green]
                        > >I used to use this as an interview question...
                        > >
                        > >When you're passing an object to a method - you're passing a variable
                        > >which points to the object. Both ByVal or ByRef pass a variable
                        > >which points to the object since a copy of an address is still the
                        > >valid address, so the object modifications persists upon leaving the
                        > >method. All ByVal does in this case is prevent you from changing the
                        > >variable pointing to the object to point to another object upon
                        > >leaving the method.[/color]
                        >
                        >[/color]


                        Comment

                        • David Anton

                          #13
                          Re: Differance between ByVal and BeRef when handling objects

                          > - HAL9000wrote:
                          Should one be allowed to write to an array that is passed by value?
                          [color=blue]
                          >
                          > If you are then that doesn't sound consistent with passing an[/color]
                          integer[color=blue]
                          > byvalue. Doesn't passing byvalue imply that you absolutely don't[/color]
                          want[color=blue]
                          > the original integer to be modified?
                          >
                          > Forrest
                          >
                          > On 22 Jul 2004 17:03:34 -0500,
                          > dave@tangibleso ftwaresolutions-dot-com.no-spam.invalid (David[/color]
                          Anton)[color=blue]
                          > wrote:
                          >
                          > I used to use this as an interview question...
                          >
                          > When you're passing an object to a method - you're passing a[/color]
                          variable[color=blue]
                          > which points to the object. Both ByVal or ByRef pass a variable
                          > which points to the object since a copy of an address is still the
                          > valid address, so the object modifications persists upon leaving[/color]
                          the[color=blue]
                          > method. All ByVal does in this case is prevent you from changing[/color]
                          the[color=blue]
                          > variable pointing to the object to point to another object upon
                          > leaving the method.[/quote:03c44a920 a][/color]

                          By all means. All that was conveyed & forced in the method
                          signature is that you can't point the original array at some other
                          array.

                          There's no nice way I know of to prevent a method from mucking about
                          with an object. Maybe there should be...
                          There is a work-around if you need to pass an object and absolutely
                          don't want the method to change anything within it - pass a copy of
                          the object created for the sole purpose of passing the object's
                          information to the method.

                          Comment

                          • - HAL9000

                            #14
                            Re: Differance between ByVal and BeRef when handling objects

                            Exactly, that is what the language does with an integer (copy it and
                            place on stack) so it would be consistent for the language to do the
                            same with an array/object.

                            I guess if we all understand what is going on then the language can
                            have quirks...

                            Forrest



                            On 23 Jul 2004 23:02:50 -0500,
                            dave@tangibleso ftwaresolutions-dot-com.no-spam.invalid (David Anton)
                            wrote:

                            < snip >
                            [color=blue]
                            >There is a work-around if you need to pass an object and absolutely
                            >don't want the method to change anything within it - pass a copy of
                            >the object created for the sole purpose of passing the object's
                            >information to the method.[/color]


                            Comment

                            • Cor Ligthert

                              #15
                              Re: Differance between ByVal and BeRef when handling objects

                              Hi Hal,

                              In addition to Jay, see as well my very easy reponse.

                              Cor


                              Comment

                              Working...