Do properties return byref or byval?

M

Monty

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!
 
M

Marina Levit [MVP]

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

Dim myLocalVar as cMyObject = someclassinstance.MyObject

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

However, if you then do something like:

myLocalVar.someProperty = "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.Substring(0,5)".
 
L

Larry Lard

Monty said:
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?

Check by examining the IL with ILDASM:

Module Module1

Sub Main()

Console.WriteLine(LeftA("brown fox", 5))
Console.WriteLine(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.VisualBasic]Microsoft.VisualBasic.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.VisualBasic]Microsoft.VisualBasic.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.
 
M

Monty

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?
 
P

Phill W.

Monty said:
Silly question: If I return an object from a property, is it returned
ByRef or ByVal?

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.Clone()
End Get
End Property
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?

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.
 
M

Marina Levit [MVP]

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.
 
M

Monty

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.
 
M

Mythran

Monty said:
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!

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$(ByVal 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 ::=
IntegerTypeCharacter |
LongTypeCharacter |
DecimalTypeCharacter |
SingleTypeCharacter |
DoubleTypeCharacter |
StringTypeCharacter

IntegerTypeCharacter ::= %

LongTypeCharacter ::= &

DecimalTypeCharacter ::= @

SingleTypeCharacter ::= !

DoubleTypeCharacter ::= #

StringTypeCharacter ::= $

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

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top