byval still allows changes to be made

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

    byval still allows changes to be made


    Hi,

    I am passing a structure to a subroutine where the passed parameter has been
    declared as ByVal.

    However, changes made to the passed variable inside the subroutine flow
    through to the actual variable that has been passed over, even though with
    ByVal this should not happen.

    Has anybody else discovered this or am I doing completely wrong /
    misunderstandin g the ByVal?

    Richard


  • Richard

    #2
    Re: byval still allows changes to be made


    My apologies, it is a CLASS not a structure that I am passing over. Still -
    VB .NET should not change it if it has been passed over as ByVal, true?

    Richard

    "Richard" <rakairnet@hotm ail.com> wrote in message
    news:c57pdf$17s 6$1@otis.netspa ce.net.au...[color=blue]
    >
    > Hi,
    >
    > I am passing a structure to a subroutine where the passed parameter has[/color]
    been[color=blue]
    > declared as ByVal.
    >
    > However, changes made to the passed variable inside the subroutine flow
    > through to the actual variable that has been passed over, even though with
    > ByVal this should not happen.
    >
    > Has anybody else discovered this or am I doing completely wrong /
    > misunderstandin g the ByVal?
    >
    > Richard
    >
    >[/color]


    Comment

    • Richard

      #3
      Re: byval still allows changes to be made


      My apologies, it is a CLASS not a structure that I am passing over. Still -
      VB .NET should not change it if it has been passed over as ByVal, true?

      Richard

      "Richard" <rakairnet@hotm ail.com> wrote in message
      news:c57pdf$17s 6$1@otis.netspa ce.net.au...[color=blue]
      >
      > Hi,
      >
      > I am passing a structure to a subroutine where the passed parameter has[/color]
      been[color=blue]
      > declared as ByVal.
      >
      > However, changes made to the passed variable inside the subroutine flow
      > through to the actual variable that has been passed over, even though with
      > ByVal this should not happen.
      >
      > Has anybody else discovered this or am I doing completely wrong /
      > misunderstandin g the ByVal?
      >
      > Richard
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: byval still allows changes to be made

        Hi Richard,

        In VB.net byval passes the value from the reference from an object and the
        value from a value.

        I hope this helps?

        Cor



        Comment

        • Cor Ligthert

          #5
          Re: byval still allows changes to be made

          Hi Richard,

          In VB.net byval passes the value from the reference from an object and the
          value from a value.

          I hope this helps?

          Cor



          Comment

          • Tom Shelton

            #6
            Re: byval still allows changes to be made

            On 2004-04-10, Richard <rakairnet@hotm ail.com> wrote:[color=blue]
            >
            > My apologies, it is a CLASS not a structure that I am passing over. Still -
            > VB .NET should not change it if it has been passed over as ByVal, true?[/color]

            Actually, it should. A class is a reference type. When a reference
            type is passed ByVal, it is the reference to the object that is passed
            by value - not the object. That means you can't change the reference to
            point to a different object (well you can locally, but the change does
            not propagate back to the caller) - but since the reference points to
            the actual object on the heap, then changes to the objects state are
            propagated back to the caller.

            --
            Tom Shelton [MVP]
            Powered By Gentoo Linux 1.4
            There are two ways to write error-free programs; only the third one works.

            Comment

            • Tom Shelton

              #7
              Re: byval still allows changes to be made

              On 2004-04-10, Richard <rakairnet@hotm ail.com> wrote:[color=blue]
              >
              > My apologies, it is a CLASS not a structure that I am passing over. Still -
              > VB .NET should not change it if it has been passed over as ByVal, true?[/color]

              Actually, it should. A class is a reference type. When a reference
              type is passed ByVal, it is the reference to the object that is passed
              by value - not the object. That means you can't change the reference to
              point to a different object (well you can locally, but the change does
              not propagate back to the caller) - but since the reference points to
              the actual object on the heap, then changes to the objects state are
              propagated back to the caller.

              --
              Tom Shelton [MVP]
              Powered By Gentoo Linux 1.4
              There are two ways to write error-free programs; only the third one works.

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: byval still allows changes to be made

                Richard,
                As the others have suggested, you are misunderstandin g Value & Reference
                Parameters when used with Value & Reference Types!

                A Class is a Reference Type, only a single instance of an object exists on
                the heap, when pass a Class variable ByVal a copy of the reference is made,
                hence the variable & parameter both refer to the same object, allowing your
                routine to make changes to the object itself.

                Remember there are two types of Parameters (ByRef & ByVal) and there are two
                types of variables (Reference Types & Value Types).

                So you can have:
                ByRef - Reference Type
                ByRef - Value Type
                ByVal - Reference Type
                ByVal - Value Type

                Lets look at types of variables:
                When you define a Class you are defining a Reference Type. Which means that
                a variable of this Class holds a reference to the object, the object itself
                exists on the heap. If I assign this variable to a second variable a copy of
                this reference is made and I now have two references to the same object on
                the heap. There is still only one object on the heap. If you define a
                Structure you are defining a Value Type. The variable itself holds the value
                of the structure. If I assign this variable to a second variable a copy of
                the entire structure is made. I now have two copies of the same structure.

                Value Types include:
                Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
                with anything defined with the Structure or Enum keyword.
                Value Types all derive directly or indirectly from System.ValueTyp e

                Reference Types include:
                Object, and anything defined with the Class, Interface or Delegate keyword
                are reference types.
                Reference Types all derive from System.Object excluding types that inherit
                from System.ValueTyp e

                Interface is a reference type, even if defined in a Structure. The structure
                itself is a value type, however if you assign the structure to a Interface
                variable, it will be Boxed, boxing places the value on the heap in a new
                object (effectively making it a reference type)

                Lets look at types of parameters:
                Now when you define a parameter to be ByVal a copy of the variable is
                passed. Remember Reference types hold a reference to the object, so passing
                a Reference Type ByVal causes a copy of this reference to be passed as a
                parameter, the single copy of the object itself is still on the heap. The
                variable & parameter both have references to this single object. Passing a
                Value Type ByVal causes a complete copy of the value to be passed as a
                parameter. Now passing a Reference Type ByRef, causes a reference to the
                variable to be passed, the variable has a reference to the object. Passing a
                Value Type ByRef also causes a reference to the variable to be passed, the
                variable has a copy of the Value.

                Remember ByVal & ByRef are how parameters are passed. Reference & Value
                Types are how quantities are stored.

                Although the following is in C# the concepts apply equally to VB.NET:



                Hope this helps
                Jay


                "Richard" <rakairnet@hotm ail.com> wrote in message
                news:c57pri$180 1$1@otis.netspa ce.net.au...[color=blue]
                >
                > My apologies, it is a CLASS not a structure that I am passing over.[/color]
                Still -[color=blue]
                > VB .NET should not change it if it has been passed over as ByVal, true?
                >
                > Richard
                >
                > "Richard" <rakairnet@hotm ail.com> wrote in message
                > news:c57pdf$17s 6$1@otis.netspa ce.net.au...[color=green]
                > >
                > > Hi,
                > >
                > > I am passing a structure to a subroutine where the passed parameter has[/color]
                > been[color=green]
                > > declared as ByVal.
                > >
                > > However, changes made to the passed variable inside the subroutine flow
                > > through to the actual variable that has been passed over, even though[/color][/color]
                with[color=blue][color=green]
                > > ByVal this should not happen.
                > >
                > > Has anybody else discovered this or am I doing completely wrong /
                > > misunderstandin g the ByVal?
                > >
                > > Richard
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: byval still allows changes to be made

                  Richard,
                  As the others have suggested, you are misunderstandin g Value & Reference
                  Parameters when used with Value & Reference Types!

                  A Class is a Reference Type, only a single instance of an object exists on
                  the heap, when pass a Class variable ByVal a copy of the reference is made,
                  hence the variable & parameter both refer to the same object, allowing your
                  routine to make changes to the object itself.

                  Remember there are two types of Parameters (ByRef & ByVal) and there are two
                  types of variables (Reference Types & Value Types).

                  So you can have:
                  ByRef - Reference Type
                  ByRef - Value Type
                  ByVal - Reference Type
                  ByVal - Value Type

                  Lets look at types of variables:
                  When you define a Class you are defining a Reference Type. Which means that
                  a variable of this Class holds a reference to the object, the object itself
                  exists on the heap. If I assign this variable to a second variable a copy of
                  this reference is made and I now have two references to the same object on
                  the heap. There is still only one object on the heap. If you define a
                  Structure you are defining a Value Type. The variable itself holds the value
                  of the structure. If I assign this variable to a second variable a copy of
                  the entire structure is made. I now have two copies of the same structure.

                  Value Types include:
                  Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
                  with anything defined with the Structure or Enum keyword.
                  Value Types all derive directly or indirectly from System.ValueTyp e

                  Reference Types include:
                  Object, and anything defined with the Class, Interface or Delegate keyword
                  are reference types.
                  Reference Types all derive from System.Object excluding types that inherit
                  from System.ValueTyp e

                  Interface is a reference type, even if defined in a Structure. The structure
                  itself is a value type, however if you assign the structure to a Interface
                  variable, it will be Boxed, boxing places the value on the heap in a new
                  object (effectively making it a reference type)

                  Lets look at types of parameters:
                  Now when you define a parameter to be ByVal a copy of the variable is
                  passed. Remember Reference types hold a reference to the object, so passing
                  a Reference Type ByVal causes a copy of this reference to be passed as a
                  parameter, the single copy of the object itself is still on the heap. The
                  variable & parameter both have references to this single object. Passing a
                  Value Type ByVal causes a complete copy of the value to be passed as a
                  parameter. Now passing a Reference Type ByRef, causes a reference to the
                  variable to be passed, the variable has a reference to the object. Passing a
                  Value Type ByRef also causes a reference to the variable to be passed, the
                  variable has a copy of the Value.

                  Remember ByVal & ByRef are how parameters are passed. Reference & Value
                  Types are how quantities are stored.

                  Although the following is in C# the concepts apply equally to VB.NET:



                  Hope this helps
                  Jay


                  "Richard" <rakairnet@hotm ail.com> wrote in message
                  news:c57pri$180 1$1@otis.netspa ce.net.au...[color=blue]
                  >
                  > My apologies, it is a CLASS not a structure that I am passing over.[/color]
                  Still -[color=blue]
                  > VB .NET should not change it if it has been passed over as ByVal, true?
                  >
                  > Richard
                  >
                  > "Richard" <rakairnet@hotm ail.com> wrote in message
                  > news:c57pdf$17s 6$1@otis.netspa ce.net.au...[color=green]
                  > >
                  > > Hi,
                  > >
                  > > I am passing a structure to a subroutine where the passed parameter has[/color]
                  > been[color=green]
                  > > declared as ByVal.
                  > >
                  > > However, changes made to the passed variable inside the subroutine flow
                  > > through to the actual variable that has been passed over, even though[/color][/color]
                  with[color=blue][color=green]
                  > > ByVal this should not happen.
                  > >
                  > > Has anybody else discovered this or am I doing completely wrong /
                  > > misunderstandin g the ByVal?
                  > >
                  > > Richard
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Cor Ligthert

                    #10
                    Re: byval still allows changes to be made

                    Hi Jay,

                    I had this week a long discussion with Jon in the dotnet general group. You
                    know I like arguing with Jon and I did give him a change in something which
                    has not real my intrest. (He is probably as serious as you however with you
                    I never should play, with Jon it is something as angling, but do not think I
                    do not respect him, I do for sure).

                    I have seen you writing about this before and at the end I told to Jon you
                    did do.
                    ---------------------
                    Now passing a Reference Type ByRef, causes a reference to the
                    variable to be passed, the variable has a reference to the object. Passing a
                    Value Type ByRef also causes a reference to the variable to be passed, the
                    variable has a copy of the Value.
                    ----------------------
                    And I said to him passing a ref is in a way passing a boxed reference (Now I
                    see this I am in doubt).

                    I also can not come clear with a NT and the heap, maybe you have a link for
                    me that I can read something about that. Why I am in doubt you think maybe,
                    that is because I cannot connect that with the 360 but more with a Unix
                    system. (And when I am sure, than I can maybe real arguing with guys like
                    you and Jon about this).

                    Cor


                    Comment

                    • Cor Ligthert

                      #11
                      Re: byval still allows changes to be made

                      Hi Jay,

                      I had this week a long discussion with Jon in the dotnet general group. You
                      know I like arguing with Jon and I did give him a change in something which
                      has not real my intrest. (He is probably as serious as you however with you
                      I never should play, with Jon it is something as angling, but do not think I
                      do not respect him, I do for sure).

                      I have seen you writing about this before and at the end I told to Jon you
                      did do.
                      ---------------------
                      Now passing a Reference Type ByRef, causes a reference to the
                      variable to be passed, the variable has a reference to the object. Passing a
                      Value Type ByRef also causes a reference to the variable to be passed, the
                      variable has a copy of the Value.
                      ----------------------
                      And I said to him passing a ref is in a way passing a boxed reference (Now I
                      see this I am in doubt).

                      I also can not come clear with a NT and the heap, maybe you have a link for
                      me that I can read something about that. Why I am in doubt you think maybe,
                      that is because I cannot connect that with the 360 but more with a Unix
                      system. (And when I am sure, than I can maybe real arguing with guys like
                      you and Jon about this).

                      Cor


                      Comment

                      • Armin Zingler

                        #12
                        Re: byval still allows changes to be made

                        "Richard" <rakairnet@hotm ail.com> schrieb[color=blue]
                        >
                        > I am passing a structure to a subroutine where the passed parameter
                        > has been declared as ByVal.
                        >
                        > However, changes made to the passed variable inside the subroutine
                        > flow through to the actual variable that has been passed over, even
                        > though with ByVal this should not happen.
                        >
                        > Has anybody else discovered this or am I doing completely wrong /
                        > misunderstandin g the ByVal?[/color]

                        1. ByVal passes a copy of the variable content.
                        2. ByRef passes a reference to the variable.

                        3. Is the variable type a value type, the variable contains the object.
                        4. Is the variable type a reference type, the variable contains the
                        reference to the object. The reference is the pointer (= an address,
                        currently 4 bytes) to the memory location containing the object.

                        That's actually all you need to know.

                        Consequently:

                        => Passing a value type byval copies the object. Changes in the called
                        procedure affect the copy, not the original.

                        => Passing a reference type byval copies the reference, so that there are
                        two references to the same object. Changing the reference in the called
                        procedure affect the copy, not the original. Changing the object changes the
                        object that's reference has been passed.

                        => Passing a value type ByRef passes a reference to the variable. This is
                        also a reference to the object. Changes to the object are changes to the
                        original object because no copy has been made. Changes to the reference do
                        not affect the original object, i.e. the local argument gets a new reference
                        to a different object.

                        => Passing a reference type ByRef passes a reference to the variable. As the
                        variable contains the reference, the passed reference is a reference to the
                        reference to the object. Changes to the object affect the original object.
                        Changes to the reference are changes to the passed variable.


                        --
                        Armin

                        How to quote and why:



                        Comment

                        • Armin Zingler

                          #13
                          Re: byval still allows changes to be made

                          "Richard" <rakairnet@hotm ail.com> schrieb[color=blue]
                          >
                          > I am passing a structure to a subroutine where the passed parameter
                          > has been declared as ByVal.
                          >
                          > However, changes made to the passed variable inside the subroutine
                          > flow through to the actual variable that has been passed over, even
                          > though with ByVal this should not happen.
                          >
                          > Has anybody else discovered this or am I doing completely wrong /
                          > misunderstandin g the ByVal?[/color]

                          1. ByVal passes a copy of the variable content.
                          2. ByRef passes a reference to the variable.

                          3. Is the variable type a value type, the variable contains the object.
                          4. Is the variable type a reference type, the variable contains the
                          reference to the object. The reference is the pointer (= an address,
                          currently 4 bytes) to the memory location containing the object.

                          That's actually all you need to know.

                          Consequently:

                          => Passing a value type byval copies the object. Changes in the called
                          procedure affect the copy, not the original.

                          => Passing a reference type byval copies the reference, so that there are
                          two references to the same object. Changing the reference in the called
                          procedure affect the copy, not the original. Changing the object changes the
                          object that's reference has been passed.

                          => Passing a value type ByRef passes a reference to the variable. This is
                          also a reference to the object. Changes to the object are changes to the
                          original object because no copy has been made. Changes to the reference do
                          not affect the original object, i.e. the local argument gets a new reference
                          to a different object.

                          => Passing a reference type ByRef passes a reference to the variable. As the
                          variable contains the reference, the passed reference is a reference to the
                          reference to the object. Changes to the object affect the original object.
                          Changes to the reference are changes to the passed variable.


                          --
                          Armin

                          How to quote and why:



                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: byval still allows changes to be made

                            Cor,[color=blue]
                            > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                            I[color=blue]
                            > see this I am in doubt).[/color]
                            Neither the "Common Language Infrastructure Annotated Standard" by James S.
                            Miller from Addision Wesley, nor the "Common Language Infrastructure
                            Standard" itself uses the term "boxed reference" that I see.

                            [color=blue]
                            > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                            I[color=blue]
                            > see this I am in doubt).[/color]
                            I'm not sure what you are actually trying to say here?

                            If you have a doubt I would recommend reading the CLI Standard. You can
                            purchase the Annotated Standard as I have, its worth having as it is the CLI
                            Standard with annotations (explainations) , or you can read the CLI Standard
                            online in the "\Program Files\Microsoft Visual Studio .NET
                            2003\SDK\v1.1\T ool Developers Guide" folder for VS.NET 2003.

                            I find it helps to have had compiler theory training when you read the CLI
                            standard.

                            Hope this helps
                            Jay

                            "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                            news:erGfRJxHEH A.164@tk2msftng p13.phx.gbl...[color=blue]
                            > Hi Jay,
                            >
                            > I had this week a long discussion with Jon in the dotnet general group.[/color]
                            You[color=blue]
                            > know I like arguing with Jon and I did give him a change in something[/color]
                            which[color=blue]
                            > has not real my intrest. (He is probably as serious as you however with[/color]
                            you[color=blue]
                            > I never should play, with Jon it is something as angling, but do not think[/color]
                            I[color=blue]
                            > do not respect him, I do for sure).
                            >
                            > I have seen you writing about this before and at the end I told to Jon you
                            > did do.
                            > ---------------------
                            > Now passing a Reference Type ByRef, causes a reference to the
                            > variable to be passed, the variable has a reference to the object. Passing[/color]
                            a[color=blue]
                            > Value Type ByRef also causes a reference to the variable to be passed, the
                            > variable has a copy of the Value.
                            > ----------------------
                            > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                            I[color=blue]
                            > see this I am in doubt).
                            >
                            > I also can not come clear with a NT and the heap, maybe you have a link[/color]
                            for[color=blue]
                            > me that I can read something about that. Why I am in doubt you think[/color]
                            maybe,[color=blue]
                            > that is because I cannot connect that with the 360 but more with a Unix
                            > system. (And when I am sure, than I can maybe real arguing with guys like
                            > you and Jon about this).
                            >
                            > Cor
                            >
                            >[/color]


                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: byval still allows changes to be made

                              Cor,[color=blue]
                              > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                              I[color=blue]
                              > see this I am in doubt).[/color]
                              Neither the "Common Language Infrastructure Annotated Standard" by James S.
                              Miller from Addision Wesley, nor the "Common Language Infrastructure
                              Standard" itself uses the term "boxed reference" that I see.

                              [color=blue]
                              > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                              I[color=blue]
                              > see this I am in doubt).[/color]
                              I'm not sure what you are actually trying to say here?

                              If you have a doubt I would recommend reading the CLI Standard. You can
                              purchase the Annotated Standard as I have, its worth having as it is the CLI
                              Standard with annotations (explainations) , or you can read the CLI Standard
                              online in the "\Program Files\Microsoft Visual Studio .NET
                              2003\SDK\v1.1\T ool Developers Guide" folder for VS.NET 2003.

                              I find it helps to have had compiler theory training when you read the CLI
                              standard.

                              Hope this helps
                              Jay

                              "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                              news:erGfRJxHEH A.164@tk2msftng p13.phx.gbl...[color=blue]
                              > Hi Jay,
                              >
                              > I had this week a long discussion with Jon in the dotnet general group.[/color]
                              You[color=blue]
                              > know I like arguing with Jon and I did give him a change in something[/color]
                              which[color=blue]
                              > has not real my intrest. (He is probably as serious as you however with[/color]
                              you[color=blue]
                              > I never should play, with Jon it is something as angling, but do not think[/color]
                              I[color=blue]
                              > do not respect him, I do for sure).
                              >
                              > I have seen you writing about this before and at the end I told to Jon you
                              > did do.
                              > ---------------------
                              > Now passing a Reference Type ByRef, causes a reference to the
                              > variable to be passed, the variable has a reference to the object. Passing[/color]
                              a[color=blue]
                              > Value Type ByRef also causes a reference to the variable to be passed, the
                              > variable has a copy of the Value.
                              > ----------------------
                              > And I said to him passing a ref is in a way passing a boxed reference (Now[/color]
                              I[color=blue]
                              > see this I am in doubt).
                              >
                              > I also can not come clear with a NT and the heap, maybe you have a link[/color]
                              for[color=blue]
                              > me that I can read something about that. Why I am in doubt you think[/color]
                              maybe,[color=blue]
                              > that is because I cannot connect that with the 360 but more with a Unix
                              > system. (And when I am sure, than I can maybe real arguing with guys like
                              > you and Jon about this).
                              >
                              > Cor
                              >
                              >[/color]


                              Comment

                              Working...