byVal Vs. byRef

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

    byVal Vs. byRef

    Hello,

    Ok here is the senerio:

    .....

    Dim myArrayList as New ArrayList(0)

    me.Test_A( myArrayList )

    myArralist.Coun t > 0 'This will be TRUE.

    Public Sub Test_A( byVal X as ArrayList )

    'Add three items.
    X.Add( "Item 1")
    X.Add( "Item 2")
    X.Add( "Item 3")

    RETURN


    When should I make the incoming parameter X as byRef?


    Thanks,
    Rob Panosh


  • William Ryan

    #2
    Re: byVal Vs. byRef

    Rob:

    I'm not sure what you are asking, but an ArrayList is a Reference type, so
    even if you pass it by value, all you are passing is a Copy of the REFERENCE
    to the object. For all intents and purposes you won't notice the
    difference. Yes, if you pass this reference type ByVal and the function
    it's passed to makes any changes to it, the original ArrayList will be
    changed. So the only difference between passing a Ref type byval or byRef
    is that in the first case you pass a copy of the Reference, in the second
    you pass the actual reference. If you want a unique copy that you can do
    with what you please, you'll probalby need to opt for a deep clone of it.

    HTH,

    Bill
    "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
    message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
    > Hello,
    >
    > Ok here is the senerio:
    >
    > ....
    >
    > Dim myArrayList as New ArrayList(0)
    >
    > me.Test_A( myArrayList )
    >
    > myArralist.Coun t > 0 'This will be TRUE.
    >
    > Public Sub Test_A( byVal X as ArrayList )
    >
    > 'Add three items.
    > X.Add( "Item 1")
    > X.Add( "Item 2")
    > X.Add( "Item 3")
    >
    > RETURN
    >
    >
    > When should I make the incoming parameter X as byRef?
    >
    >
    > Thanks,
    > Rob Panosh
    >
    >[/color]


    Comment

    • Brian Henry

      #3
      Re: byVal Vs. byRef

      that should be a byref if you are going to make changes to it that will be
      visible outside of that sub... byval only makes a copy of the data.. byref
      makes a pointer to the data, which is what you need if you are going to make
      changes that are visible outside of that test sub, or they will just get
      thrown out as the sub exits... and why do you have return in there?


      "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
      message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Hello,
      >
      > Ok here is the senerio:
      >
      > ....
      >
      > Dim myArrayList as New ArrayList(0)
      >
      > me.Test_A( myArrayList )
      >
      > myArralist.Coun t > 0 'This will be TRUE.
      >
      > Public Sub Test_A( byVal X as ArrayList )
      >
      > 'Add three items.
      > X.Add( "Item 1")
      > X.Add( "Item 2")
      > X.Add( "Item 3")
      >
      > RETURN
      >
      >
      > When should I make the incoming parameter X as byRef?
      >
      >
      > Thanks,
      > Rob Panosh
      >
      >[/color]


      Comment

      • Rob Panosh

        #4
        Re: byVal Vs. byRef

        William,
        [color=blue]
        > even if you pass it by value, all you are passing is a Copy of the[/color]
        REFERENCE[color=blue]
        > to the object.[/color]
        That is what I was asking. I could see a difference between them. So are
        there any peformance implications here?
        My guess byRef would have better performance because it doesn't have to make
        a copied of the reference.

        Thanks for your help and timely reponse

        Rob


        "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
        news:uSVGYVywDH A.1740@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Rob:
        >
        > I'm not sure what you are asking, but an ArrayList is a Reference type, so
        > even if you pass it by value, all you are passing is a Copy of the[/color]
        REFERENCE[color=blue]
        > to the object. For all intents and purposes you won't notice the
        > difference. Yes, if you pass this reference type ByVal and the function
        > it's passed to makes any changes to it, the original ArrayList will be
        > changed. So the only difference between passing a Ref type byval or byRef
        > is that in the first case you pass a copy of the Reference, in the second
        > you pass the actual reference. If you want a unique copy that you can do
        > with what you please, you'll probalby need to opt for a deep clone of it.
        >
        > HTH,
        >
        > Bill
        > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
        > message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
        > > Hello,
        > >
        > > Ok here is the senerio:
        > >
        > > ....
        > >
        > > Dim myArrayList as New ArrayList(0)
        > >
        > > me.Test_A( myArrayList )
        > >
        > > myArralist.Coun t > 0 'This will be TRUE.
        > >
        > > Public Sub Test_A( byVal X as ArrayList )
        > >
        > > 'Add three items.
        > > X.Add( "Item 1")
        > > X.Add( "Item 2")
        > > X.Add( "Item 3")
        > >
        > > RETURN
        > >
        > >
        > > When should I make the incoming parameter X as byRef?
        > >
        > >
        > > Thanks,
        > > Rob Panosh
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Rob Panosh

          #5
          Re: byVal Vs. byRef

          > that should be a byref if you are going to make changes to it that will be
          It doesn't make any difference how I pass it I can still see the changes.
          [color=blue]
          > thrown out as the sub exits... and why do you have return in there?[/color]
          fat fingers .... didn't mean to put it there.

          rob

          "Brian Henry" <brianiup[nospam]@adelphia.net> wrote in message
          news:e4eAJWywDH A.2000@TK2MSFTN GP11.phx.gbl...[color=blue]
          > that should be a byref if you are going to make changes to it that will be
          > visible outside of that sub... byval only makes a copy of the data.. byref
          > makes a pointer to the data, which is what you need if you are going to[/color]
          make[color=blue]
          > changes that are visible outside of that test sub, or they will just get
          > thrown out as the sub exits... and why do you have return in there?
          >
          >
          > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
          > message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
          > > Hello,
          > >
          > > Ok here is the senerio:
          > >
          > > ....
          > >
          > > Dim myArrayList as New ArrayList(0)
          > >
          > > me.Test_A( myArrayList )
          > >
          > > myArralist.Coun t > 0 'This will be TRUE.
          > >
          > > Public Sub Test_A( byVal X as ArrayList )
          > >
          > > 'Add three items.
          > > X.Add( "Item 1")
          > > X.Add( "Item 2")
          > > X.Add( "Item 3")
          > >
          > > RETURN
          > >
          > >
          > > When should I make the incoming parameter X as byRef?
          > >
          > >
          > > Thanks,
          > > Rob Panosh
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Glen Conway

            #6
            Re: byVal Vs. byRef

            Rob,

            If Test_A was function and you were assigning an array list back to
            myArrayList then ByVal is OK. If it's a sub and you want the original
            object modified then pass it as ByRef. In this instance, Test_A only
            creates a new ArrayList object (X), adds some items and then does nothing,
            myArrayList isn't modified at all.

            Hope this helps

            Glen

            "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
            message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
            > Hello,
            >
            > Ok here is the senerio:
            >
            > ....
            >
            > Dim myArrayList as New ArrayList(0)
            >
            > me.Test_A( myArrayList )
            >
            > myArralist.Coun t > 0 'This will be TRUE.
            >
            > Public Sub Test_A( byVal X as ArrayList )
            >
            > 'Add three items.
            > X.Add( "Item 1")
            > X.Add( "Item 2")
            > X.Add( "Item 3")
            >
            > RETURN
            >
            >
            > When should I make the incoming parameter X as byRef?
            >
            >
            > Thanks,
            > Rob Panosh
            >
            >[/color]


            Comment

            • Rob Panosh

              #7
              Re: byVal Vs. byRef

              William,
              [color=blue]
              > even if you pass it by value, all you are passing is a Copy of the[/color]
              REFERENCE[color=blue]
              > to the object.[/color]
              That is what I was asking. I could see a difference between them. So are
              there any peformance implications here?
              My guess byRef would have better performance because it doesn't have to make
              a copied of the reference.

              Thanks for your help and timely reponse

              Rob


              "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
              news:uSVGYVywDH A.1740@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Rob:
              >
              > I'm not sure what you are asking, but an ArrayList is a Reference type, so
              > even if you pass it by value, all you are passing is a Copy of the[/color]
              REFERENCE[color=blue]
              > to the object. For all intents and purposes you won't notice the
              > difference. Yes, if you pass this reference type ByVal and the function
              > it's passed to makes any changes to it, the original ArrayList will be
              > changed. So the only difference between passing a Ref type byval or byRef
              > is that in the first case you pass a copy of the Reference, in the second
              > you pass the actual reference. If you want a unique copy that you can do
              > with what you please, you'll probalby need to opt for a deep clone of it.
              >
              > HTH,
              >
              > Bill
              > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
              > message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
              > > Hello,
              > >
              > > Ok here is the senerio:
              > >
              > > ....
              > >
              > > Dim myArrayList as New ArrayList(0)
              > >
              > > me.Test_A( myArrayList )
              > >
              > > myArralist.Coun t > 0 'This will be TRUE.
              > >
              > > Public Sub Test_A( byVal X as ArrayList )
              > >
              > > 'Add three items.
              > > X.Add( "Item 1")
              > > X.Add( "Item 2")
              > > X.Add( "Item 3")
              > >
              > > RETURN
              > >
              > >
              > > When should I make the incoming parameter X as byRef?
              > >
              > >
              > > Thanks,
              > > Rob Panosh
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Rob Panosh

                #8
                Re: byVal Vs. byRef

                > that should be a byref if you are going to make changes to it that will be
                It doesn't make any difference how I pass it I can still see the changes.
                [color=blue]
                > thrown out as the sub exits... and why do you have return in there?[/color]
                fat fingers .... didn't mean to put it there.

                rob

                "Brian Henry" <brianiup[nospam]@adelphia.net> wrote in message
                news:e4eAJWywDH A.2000@TK2MSFTN GP11.phx.gbl...[color=blue]
                > that should be a byref if you are going to make changes to it that will be
                > visible outside of that sub... byval only makes a copy of the data.. byref
                > makes a pointer to the data, which is what you need if you are going to[/color]
                make[color=blue]
                > changes that are visible outside of that test sub, or they will just get
                > thrown out as the sub exits... and why do you have return in there?
                >
                >
                > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                > message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
                > > Hello,
                > >
                > > Ok here is the senerio:
                > >
                > > ....
                > >
                > > Dim myArrayList as New ArrayList(0)
                > >
                > > me.Test_A( myArrayList )
                > >
                > > myArralist.Coun t > 0 'This will be TRUE.
                > >
                > > Public Sub Test_A( byVal X as ArrayList )
                > >
                > > 'Add three items.
                > > X.Add( "Item 1")
                > > X.Add( "Item 2")
                > > X.Add( "Item 3")
                > >
                > > RETURN
                > >
                > >
                > > When should I make the incoming parameter X as byRef?
                > >
                > >
                > > Thanks,
                > > Rob Panosh
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Glen Conway

                  #9
                  Re: byVal Vs. byRef

                  Rob,

                  If Test_A was function and you were assigning an array list back to
                  myArrayList then ByVal is OK. If it's a sub and you want the original
                  object modified then pass it as ByRef. In this instance, Test_A only
                  creates a new ArrayList object (X), adds some items and then does nothing,
                  myArrayList isn't modified at all.

                  Hope this helps

                  Glen

                  "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                  message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
                  > Hello,
                  >
                  > Ok here is the senerio:
                  >
                  > ....
                  >
                  > Dim myArrayList as New ArrayList(0)
                  >
                  > me.Test_A( myArrayList )
                  >
                  > myArralist.Coun t > 0 'This will be TRUE.
                  >
                  > Public Sub Test_A( byVal X as ArrayList )
                  >
                  > 'Add three items.
                  > X.Add( "Item 1")
                  > X.Add( "Item 2")
                  > X.Add( "Item 3")
                  >
                  > RETURN
                  >
                  >
                  > When should I make the incoming parameter X as byRef?
                  >
                  >
                  > Thanks,
                  > Rob Panosh
                  >
                  >[/color]


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: byVal Vs. byRef

                    Rob,
                    In addition to the others comments:[color=blue]
                    > When should I make the incoming parameter X as byRef?[/color]
                    Short answer: Only when you need to modify the caller's variable!

                    Long answer:
                    ByVal & ByRef Parameters are independent of Reference & Value Types. All
                    parameters 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.

                    Less memory use & better performance should not be a factor in choosing
                    ByVal & ByRef. The only time to consider ByRef for less memory & performance
                    is when passing large structures (structures as in defined with the
                    Structure keyword), however structures should never be large!

                    Structure Usage Guidelines.


                    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



                    "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                    message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
                    > Hello,
                    >
                    > Ok here is the senerio:
                    >
                    > ....
                    >
                    > Dim myArrayList as New ArrayList(0)
                    >
                    > me.Test_A( myArrayList )
                    >
                    > myArralist.Coun t > 0 'This will be TRUE.
                    >
                    > Public Sub Test_A( byVal X as ArrayList )
                    >
                    > 'Add three items.
                    > X.Add( "Item 1")
                    > X.Add( "Item 2")
                    > X.Add( "Item 3")
                    >
                    > RETURN
                    >
                    >
                    > When should I make the incoming parameter X as byRef?
                    >
                    >
                    > Thanks,
                    > Rob Panosh
                    >
                    >[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: byVal Vs. byRef

                      Rob,
                      In addition to the others comments:[color=blue]
                      > When should I make the incoming parameter X as byRef?[/color]
                      Short answer: Only when you need to modify the caller's variable!

                      Long answer:
                      ByVal & ByRef Parameters are independent of Reference & Value Types. All
                      parameters 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.

                      Less memory use & better performance should not be a factor in choosing
                      ByVal & ByRef. The only time to consider ByRef for less memory & performance
                      is when passing large structures (structures as in defined with the
                      Structure keyword), however structures should never be large!

                      Structure Usage Guidelines.


                      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



                      "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                      message news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
                      > Hello,
                      >
                      > Ok here is the senerio:
                      >
                      > ....
                      >
                      > Dim myArrayList as New ArrayList(0)
                      >
                      > me.Test_A( myArrayList )
                      >
                      > myArralist.Coun t > 0 'This will be TRUE.
                      >
                      > Public Sub Test_A( byVal X as ArrayList )
                      >
                      > 'Add three items.
                      > X.Add( "Item 1")
                      > X.Add( "Item 2")
                      > X.Add( "Item 3")
                      >
                      > RETURN
                      >
                      >
                      > When should I make the incoming parameter X as byRef?
                      >
                      >
                      > Thanks,
                      > Rob Panosh
                      >
                      >[/color]


                      Comment

                      • David Browne

                        #12
                        Re: byVal Vs. byRef


                        "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                        message news:eZBuUdywDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
                        > William,
                        >[color=green]
                        > > even if you pass it by value, all you are passing is a Copy of the[/color]
                        > REFERENCE[color=green]
                        > > to the object.[/color]
                        > That is what I was asking. I could see a difference between them. So are
                        > there any peformance implications here?
                        > My guess byRef would have better performance because it doesn't have to[/color]
                        make[color=blue]
                        > a copied of the reference.
                        >[/color]

                        No. A reference is essentially a pointer. A 32-bit argument passed on the
                        stack. It doesn't get any cheaper than that.

                        David


                        Comment

                        • David Browne

                          #13
                          Re: byVal Vs. byRef


                          "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> wrote in
                          message news:eZBuUdywDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
                          > William,
                          >[color=green]
                          > > even if you pass it by value, all you are passing is a Copy of the[/color]
                          > REFERENCE[color=green]
                          > > to the object.[/color]
                          > That is what I was asking. I could see a difference between them. So are
                          > there any peformance implications here?
                          > My guess byRef would have better performance because it doesn't have to[/color]
                          make[color=blue]
                          > a copied of the reference.
                          >[/color]

                          No. A reference is essentially a pointer. A 32-bit argument passed on the
                          stack. It doesn't get any cheaper than that.

                          David


                          Comment

                          • Armin Zingler

                            #14
                            Re: byVal Vs. byRef

                            "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om>
                            schrieb[color=blue]
                            > My guess byRef would have better performance because it doesn't have
                            > to make a copied of the reference.[/color]

                            Performance difference byval and byref when passing reference types is close
                            to zero (or _is_ zero). Both store a 4-byte value on the stack.

                            In a procedure, when accessing an object passed to the procedure, ByVal is a
                            little bit faster - but this is also close to zero (but is not zero).


                            --
                            Armin




                            Comment

                            • Armin Zingler

                              #15
                              Re: byVal Vs. byRef

                              "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om>
                              schrieb[color=blue]
                              > My guess byRef would have better performance because it doesn't have
                              > to make a copied of the reference.[/color]

                              Performance difference byval and byref when passing reference types is close
                              to zero (or _is_ zero). Both store a 4-byte value on the stack.

                              In a procedure, when accessing an object passed to the procedure, ByVal is a
                              little bit faster - but this is also close to zero (but is not zero).


                              --
                              Armin




                              Comment

                              Working...