ByRef or ByVal

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

    ByRef or ByVal

    Hi,

    i know the difference of ByRef and ByVal, in case if use byref or byval
    don't affect the result which one should prefer? (less memory use, better
    performance ....issue)

    thx


  • Klaus H. Probst

    #2
    Re: ByRef or ByVal

    They are not the same thing. What do you mean "which one should prefer"? Use
    the one that you need to use.

    For example:


    (watch for wrapping)


    --
    _______________ _____
    Klaus H. Probst, MVP



    "Hei" <chun_hei@msn.c om> wrote in message
    news:eVR$JyYrDH A.2416@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi,
    >
    > i know the difference of ByRef and ByVal, in case if use byref or byval
    > don't affect the result which one should prefer? (less memory use, better
    > performance ....issue)
    >
    > thx
    >
    >[/color]


    Comment

    • Armin Zingler

      #3
      Re: ByRef or ByVal

      "Hei" <chun_hei@msn.c om> schrieb[color=blue]
      >
      > i know the difference of ByRef and ByVal, in case if use byref or
      > byval don't affect the result which one should prefer? (less memory
      > use, better performance ....issue)[/color]

      My personal basic rule: If the procedure shouldn't change the passed value,
      declare it ByVal, otherwise ByRef.

      The "passed value" is the reference to an object if it is a reference type.
      The "passed value" is the object itself if it is a value type.


      --
      Armin




      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: ByRef or ByVal

        * "Hei" <chun_hei@msn.c om> scripsit:[color=blue]
        > i know the difference of ByRef and ByVal, in case if use byref or byval
        > don't affect the result which one should prefer? (less memory use, better
        > performance ....issue)[/color]

        There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET, 'ByVal'
        is used by default because it often makes more sense. 'ByRef' is only
        relevant for reference types, if the _reference_ itself should be
        changed by the procedure.

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        • Armin Zingler

          #5
          Re: ByRef or ByVal

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb[color=blue]
          > * "Hei" <chun_hei@msn.c om> scripsit:[color=green]
          > > i know the difference of ByRef and ByVal, in case if use byref or
          > > byval don't affect the result which one should prefer? (less memory
          > > use, better performance ....issue)[/color]
          >
          > There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET,
          > 'ByVal' is used by default because it often makes more sense.
          > 'ByRef' is only relevant for reference types, if the _reference_
          > itself should be changed by the procedure.[/color]


          Why do you think that ByRef is only relevant for reference types??


          --
          Armin

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: ByRef or ByVal

            * "Armin Zingler" <az.nospam@free net.de> scripsit:[color=blue][color=green][color=darkred]
            >>> i know the difference of ByRef and ByVal, in case if use byref or
            >>> byval don't affect the result which one should prefer? (less memory
            >>> use, better performance ....issue)[/color]
            >>
            >> There _is_ a difference between 'ByVal' and 'ByRef'. In VB.NET,
            >> 'ByVal' is used by default because it often makes more sense.
            >> 'ByRef' is only relevant for reference types, if the _reference_
            >> itself should be changed by the procedure.[/color]
            >
            > Why do you think that ByRef is only relevant for reference types??[/color]

            Maybe my bad skills in the English language caused the misinterpretati on
            on your side. If wanted to point out when 'ByRef' makes sense when used
            with reference types.

            --
            Herfried K. Wagner [MVP]
            <http://www.mvps.org/dotnet>

            Comment

            • Armin Zingler

              #7
              Re: ByRef or ByVal

              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb[color=blue]
              > * "Armin Zingler" <az.nospam@free net.de> scripsit:[color=green][color=darkred]
              > >>> i know the difference of ByRef and ByVal, in case if use byref
              > >>> or byval don't affect the result which one should prefer? (less
              > >>> memory use, better performance ....issue)
              > >>
              > >> There _is_ a difference between 'ByVal' and 'ByRef'. In
              > >> VB.NET, 'ByVal' is used by default because it often makes more
              > >> sense. 'ByRef' is only relevant for reference types, if the
              > >> _reference_ itself should be changed by the procedure.[/color]
              > >
              > > Why do you think that ByRef is only relevant for reference
              > > types??[/color]
              >
              > Maybe my bad skills in the English language[/color]

              haha.. ;)
              [color=blue]
              > caused the
              > misinterpretati on on your side. If wanted to point out when 'ByRef'
              > makes sense when used with reference types.[/color]

              I still don't understand - but, no need for an explanation. We know what
              byval/byref do. ;-)


              --
              Armin




              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: ByRef or ByVal

                Hei,[color=blue]
                > i know the difference of ByRef and ByVal, in case if use byref or byval
                > don't affect the result which one should prefer? (less memory use, better
                > performance ....issue)[/color]
                Are you sure you know the difference? ;-)

                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


                "Hei" <chun_hei@msn.c om> wrote in message
                news:eVR$JyYrDH A.2416@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Hi,
                >
                > i know the difference of ByRef and ByVal, in case if use byref or byval
                > don't affect the result which one should prefer? (less memory use, better
                > performance ....issue)
                >
                > thx
                >
                >[/color]


                Comment

                Working...