byVal Vs. byRef

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

    #16
    Re: byVal Vs. byRef

    "Armin Zingler" <az.nospam@free net.de> schrieb[color=blue]
    > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om>
    > schrieb[color=green]
    > > 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.[/color]

    Should be: ....difference between byval and byref...


    In addition: The difference when passing value types depends on the size of
    the value type. Passing a value type ByRef always stores 4 byte on the
    stack. Passing it ByVal stores the whole object on the stack, so the bigger
    the value type, the slower. But, as it has already been mentioned, the
    decision to use ByVal or ByRef should not depend on this, but on whether the
    passed *variable* should be changable (and the variable contains a reference
    with reference types and the whole object with value types...)

    --
    Armin




    Comment

    • Armin Zingler

      #17
      Re: byVal Vs. byRef

      "Armin Zingler" <az.nospam@free net.de> schrieb[color=blue]
      > "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om>
      > schrieb[color=green]
      > > 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.[/color]

      Should be: ....difference between byval and byref...


      In addition: The difference when passing value types depends on the size of
      the value type. Passing a value type ByRef always stores 4 byte on the
      stack. Passing it ByVal stores the whole object on the stack, so the bigger
      the value type, the slower. But, as it has already been mentioned, the
      decision to use ByVal or ByRef should not depend on this, but on whether the
      passed *variable* should be changable (and the variable contains a reference
      with reference types and the whole object with value types...)

      --
      Armin




      Comment

      • Codemonkey

        #18
        Re: byVal Vs. byRef

        Rob,

        Basically, there are two types of variables: value types and reference
        types. Value types consist simply of a value (integer, float etc.).
        Reference types consist of two things - the object and the pointer to the
        object (your variable name). When you pass a reference type by value, you
        copy the pointer, not the object.

        As the ArrayList type is a *reference* object, the variable you named
        "myArrayLis t" is an object pointer that points to somewhere in memory that
        contains your ArrayList.

        When you pass "myArrayLis t" By Value to a function, the object pointer is
        copied, but still points to the same object in memory - this is why you can
        change the contents of the ArrayList. In your function, if you set "X" to a
        new Arraylist, this will not be reflected by the "myArrayLis t" variable as
        you passed it by value - it will still be the same ArrayList instance.

        However, if you pass "myArrayLis t" by Reference, you pass the "myArrayLis t"
        object pointer. Again, you can modify the contents, but this time, if you
        set X to a new ArrayList, "myArrayLis t" will also be set to the new
        ArrayList because you passed the object pointer by reference.

        Hope this is slightly clearer than mud ;)

        Trev.




        "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

        • Codemonkey

          #19
          Re: byVal Vs. byRef

          Rob,

          Basically, there are two types of variables: value types and reference
          types. Value types consist simply of a value (integer, float etc.).
          Reference types consist of two things - the object and the pointer to the
          object (your variable name). When you pass a reference type by value, you
          copy the pointer, not the object.

          As the ArrayList type is a *reference* object, the variable you named
          "myArrayLis t" is an object pointer that points to somewhere in memory that
          contains your ArrayList.

          When you pass "myArrayLis t" By Value to a function, the object pointer is
          copied, but still points to the same object in memory - this is why you can
          change the contents of the ArrayList. In your function, if you set "X" to a
          new Arraylist, this will not be reflected by the "myArrayLis t" variable as
          you passed it by value - it will still be the same ArrayList instance.

          However, if you pass "myArrayLis t" by Reference, you pass the "myArrayLis t"
          object pointer. Again, you can modify the contents, but this time, if you
          set X to a new ArrayList, "myArrayLis t" will also be set to the new
          ArrayList because you passed the object pointer by reference.

          Hope this is slightly clearer than mud ;)

          Trev.




          "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

          • José Manuel Agüero

            #20
            Re: byVal Vs. byRef

            Hello, Rob:

            Codemonkey has given an excellent explanation. Here is a practical case:

            '\\\
            Public Sub Test_A( byVal X as ArrayList )

            'Add three items when passed ByVal or ByRef.
            X.Add( "Item 1")
            X.Add( "Item 2")
            X.Add( "Item 3")

            'This will be useless using ByVal, but will change the original X when passed ByRef:
            Dim Y As New ArrayList
            Y.Add( "Item 4")
            Y.Add( "Item 5")
            Y.Add( "Item 6")
            X=Y

            End Sub
            '///

            Regards.


            "Rob Panosh" <rob_!!!NO!!!SP AM!!!_panosh@as dsoftadfdware.c om> escribió en el mensaje news:OmsdbMywDH A.556@TK2MSFTNG P11.phx.gbl...
            | 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

            Comment

            Working...