By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Do properties return byref or byval?

P: n/a
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!
Mar 30 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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)".

"Monty" <mo***@community.nospam> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
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!

Mar 30 '06 #2

P: n/a

Monty wrote:
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.

--
Larry Lard
Replies to group please

Mar 30 '06 #3

P: n/a
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?
Mar 30 '06 #4

P: n/a
"Monty" <mo***@community.nospam> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
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.
Mar 30 '06 #5

P: n/a
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" <mo***@community.nospam> wrote in message
news:Ot**************@TK2MSFTNGP09.phx.gbl...
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?

Mar 30 '06 #6

P: n/a
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]" <so*****@nospam.com> wrote in message
news:eN*************@TK2MSFTNGP09.phx.gbl...
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.

Mar 30 '06 #7

P: n/a

"Monty" <mo***@community.nospam> wrote in message
news:OV**************@TK2MSFTNGP14.phx.gbl...
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

Mar 30 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.