Do properties return byref or byval?

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

    #1

    Do properties return byref or byval?

    Silly question: If I return an object from a property, is it returned ByRef
    or ByVal? Is there a way to specify one way or the other? For instance, will
    the code that calls the MyObject() property below get a cMyObject as ByRef
    or ByVal? Thanks.

    Public ReadOnly Property MyObject() As cMyObject
    Get
    Return moMyObject
    End Get
    End Property

    Bonus question, completely unrelated: In VB.Old I used to put a dollar sign
    ("$") after a string function to force a string value return rather than a
    variant. For instance, "Left(s, 5)" will return a variant, but "Left$(s, 5)"
    will always return a string. I notice I still have the option of appending a
    dollar sign, but the "Left" function already returns a string type. Does
    adding the dollar sign make any difference in VB.Net?

    TIA!


  • Marina Levit [MVP]

    #2
    Re: Do properties return byref or byval?

    This would be 'ByVal'. As in, something like:

    Dim myLocalVar as cMyObject = someclassinstan ce.MyObject

    If you then change where 'myLocalVar' points to, the variable 'moMyObject'
    will not be affected.

    However, if you then do something like:

    myLocalVar.some Property = "hello"

    Since both myLocalVar and moMyObject still point to the same actual instance
    of an object, then someProperty will always be the same.

    My concern is that because you are asking this you don't have a proper
    understand of what ByVal and ByRef really means, and you may think ByVal
    creates a copy of the object - which it never does.

    I don't know about the $ sign, there is no such thing as Variant in .NET. I
    recommend you abandon all those function from VB6 that they ported to .NET.
    The String class has all the methods and properties you need to do string
    manipulation. For example, to get the first 5 characters of a string you
    would say "myString.Subst ring(0,5)".

    "Monty" <monty@communit y.nospam> wrote in message
    news:OVyHaaAVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Silly question: If I return an object from a property, is it returned
    > ByRef or ByVal? Is there a way to specify one way or the other? For
    > instance, will the code that calls the MyObject() property below get a
    > cMyObject as ByRef or ByVal? Thanks.
    >
    > Public ReadOnly Property MyObject() As cMyObject
    > Get
    > Return moMyObject
    > End Get
    > End Property
    >
    > Bonus question, completely unrelated: In VB.Old I used to put a dollar
    > sign ("$") after a string function to force a string value return rather
    > than a variant. For instance, "Left(s, 5)" will return a variant, but
    > "Left$(s, 5)" will always return a string. I notice I still have the
    > option of appending a dollar sign, but the "Left" function already returns
    > a string type. Does adding the dollar sign make any difference in VB.Net?
    >
    > TIA!
    >[/color]


    Comment

    • Larry Lard

      #3
      Re: Do properties return byref or byval?


      Monty wrote:[color=blue]
      > Bonus question, completely unrelated: In VB.Old I used to put a dollar sign
      > ("$") after a string function to force a string value return rather than a
      > variant. For instance, "Left(s, 5)" will return a variant, but "Left$(s, 5)"
      > will always return a string. I notice I still have the option of appending a
      > dollar sign, but the "Left" function already returns a string type. Does
      > adding the dollar sign make any difference in VB.Net?[/color]

      Check by examining the IL with ILDASM:

      Module Module1

      Sub Main()

      Console.WriteLi ne(LeftA("brown fox", 5))
      Console.WriteLi ne(LeftB("lazy dog", 4))
      End Sub

      Private Function LeftA(ByVal s As String, ByVal i As Integer) As
      String
      Return Left(s, i)
      End Function

      Private Function LeftB(ByVal s As String, ByVal i As Integer) As
      String
      Return Left$(s, i)
      End Function

      End Module

      IL:
      ..method private static string LeftA(string s,
      int32 i) cil managed
      {
      // Code size 13 (0xd)
      .maxstack 2
      .locals init ([0] string LeftA)
      IL_0000: nop
      IL_0001: ldarg.0
      IL_0002: ldarg.1
      IL_0003: call string
      [Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :Left(string,

      int32)
      IL_0008: stloc.0
      IL_0009: br.s IL_000b
      IL_000b: ldloc.0
      IL_000c: ret
      } // end of method Module1::LeftA

      ..method private static string LeftB(string s,
      int32 i) cil managed
      {
      // Code size 13 (0xd)
      .maxstack 2
      .locals init ([0] string LeftB)
      IL_0000: nop
      IL_0001: ldarg.0
      IL_0002: ldarg.1
      IL_0003: call string
      [Microsoft.Visua lBasic]Microsoft.Visua lBasic.Strings: :Left(string,

      int32)
      IL_0008: stloc.0
      IL_0009: br.s IL_000b
      IL_000b: ldloc.0
      IL_000c: ret
      } // end of method Module1::LeftB

      No difference.

      --
      Larry Lard
      Replies to group please

      Comment

      • Monty

        #4
        Re: Do properties return byref or byval?

        Thanks Marina, you're right, I had to go back and refresh on ByVal and ByRef
        for reference types. You've answered my original question, but just out of
        curiosity, is it possible to return a reference type object from a property
        as ByRef?

        Yep, I know variants are gone and I'm familiar with the new string
        functions, but even they support (perhaps 'allow' is a better word?) a
        dollar sign being appended. I just wondered if it has any practical
        difference. For instance:

        Dim sTest As String = "YaddaYadda "
        sTest = sTest.Substring $(3) '<-- does this "$" do anything?


        Comment

        • Phill  W.

          #5
          Re: Do properties return byref or byval?

          "Monty" <monty@communit y.nospam> wrote in message
          news:OVyHaaAVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Silly question: If I return an object from a property, is it returned
          > ByRef or ByVal?[/color]

          If the property uses a Reference Type (as in your example), then it returns
          a reference to the object. Change properties through that reference and
          you change the original object.
          If you really need to avoid this (and it's usually not worth the effort),
          return
          a Clone() of the original object.

          Public ReadOnly Property MyObject() As cMyObject
          Get
          ' Wasteful, potentially slow and usually unnecessary
          Return moMyObject.Clon e()
          End Get
          End Property
          [color=blue]
          > In VB.Old I used to put a dollar sign ("$") after a string function Does
          > adding the dollar sign make any difference in VB.Net?[/color]

          Yes. It makes your code horrible to read. :-)

          Variants are dead and buried (and good riddance) in .Net so Left()
          will /always/ return you a String, no matter what you try to decorate
          it with.

          Better still, step up and use the methods on the String class.

          s.SubString( 0, 5 )

          Mind you, watch out for things like

          "abc".SubString ( 0, 5 )

          ;-)

          HTH,
          Phill W.


          Comment

          • Marina Levit [MVP]

            #6
            Re: Do properties return byref or byval?

            Forget the whole $ sign thing. Pretend it never existed - it will make all
            your questions go away.

            Substring will always return a String. There is no concept of the $ sign
            functions in .NET.

            "Monty" <monty@communit y.nospam> wrote in message
            news:OtXl7EBVGH A.4952@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Thanks Marina, you're right, I had to go back and refresh on ByVal and
            > ByRef for reference types. You've answered my original question, but just
            > out of curiosity, is it possible to return a reference type object from a
            > property as ByRef?
            >
            > Yep, I know variants are gone and I'm familiar with the new string
            > functions, but even they support (perhaps 'allow' is a better word?) a
            > dollar sign being appended. I just wondered if it has any practical
            > difference. For instance:
            >
            > Dim sTest As String = "YaddaYadda "
            > sTest = sTest.Substring $(3) '<-- does this "$" do anything?
            >[/color]


            Comment

            • Monty

              #7
              Re: Do properties return byref or byval?

              Pretending doesn't make my questions go away, but understanding does. I
              think Larry nailed the answer to this, and showed me how to help answer some
              questions like these myself. Thank you all for your responses.


              "Marina Levit [MVP]" <someone@nospam .com> wrote in message
              news:eNfTDbBVGH A.776@TK2MSFTNG P09.phx.gbl...[color=blue]
              > Forget the whole $ sign thing. Pretend it never existed - it will make
              > all your questions go away.
              >
              > Substring will always return a String. There is no concept of the $ sign
              > functions in .NET.[/color]


              Comment

              • Mythran

                #8
                Re: Do properties return byref or byval?


                "Monty" <monty@communit y.nospam> wrote in message
                news:OVyHaaAVGH A.4792@TK2MSFTN GP14.phx.gbl...[color=blue]
                > Silly question: If I return an object from a property, is it returned
                > ByRef or ByVal? Is there a way to specify one way or the other? For
                > instance, will the code that calls the MyObject() property below get a
                > cMyObject as ByRef or ByVal? Thanks.
                >
                > Public ReadOnly Property MyObject() As cMyObject
                > Get
                > Return moMyObject
                > End Get
                > End Property
                >
                > Bonus question, completely unrelated: In VB.Old I used to put a dollar
                > sign ("$") after a string function to force a string value return rather
                > than a variant. For instance, "Left(s, 5)" will return a variant, but
                > "Left$(s, 5)" will always return a string. I notice I still have the
                > option of appending a dollar sign, but the "Left" function already returns
                > a string type. Does adding the dollar sign make any difference in VB.Net?
                >
                > TIA!
                >[/color]

                I noticed that everyone was thinking the $ had something to do with the
                Variance data type (which doesn't exist in .Net). In .Net, the $ does not
                mean "variant string". It's a what is called a Type Character (MSDN). It
                can be used in declaring the type of the method/property/member:

                Module Module2
                ' This function takes a Long parameter and returns a String.
                Function StringFunc$(ByV al LongParam&)
                ' The following line causes an error because the type
                ' character conflicts with the declared type of
                ' StringFunc and LongParam.
                StringFunc# = CStr(LongParam@ )

                ' The following line is valid.
                StringFunc$ = CStr(LongParam& )
                End Function
                End Module

                TypeCharacter ::=
                IntegerTypeChar acter |
                LongTypeCharact er |
                DecimalTypeChar acter |
                SingleTypeChara cter |
                DoubleTypeChara cter |
                StringTypeChara cter

                IntegerTypeChar acter ::= %

                LongTypeCharact er ::= &

                DecimalTypeChar acter ::= @

                SingleTypeChara cter ::= !

                DoubleTypeChara cter ::= #

                StringTypeChara cter ::= $

                This was pulled from the MSDN documentation (Visual Basic Language
                Specification: 2.2.1 Type Characters) @ MSDN Help
                (ms-help://MS.MSDNQTR.2003 FEB.1033/vbls7/html/vblrfVBSpec2_2_ 1.htm) which is
                the documentation for Visual Basic .Net 2003. :) whew, mouthfull.

                HTH,
                Mythran

                Comment

                Working...