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! 7 2893
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" <mo***@communit y.nospam> wrote in message
news:OV******** ******@TK2MSFTN GP14.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!
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.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
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?
"Monty" <mo***@communit y.nospam> wrote in message
news:OV******** ******@TK2MSFTN GP14.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.Clon e()
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.
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***@communit y.nospam> wrote in message
news:Ot******** ******@TK2MSFTN GP09.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?
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******** *****@TK2MSFTNG P09.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.
"Monty" <mo***@communit y.nospam> wrote in message
news:OV******** ******@TK2MSFTN GP14.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$(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christopher M. Lauer |
last post by:
I have done my best to answer this question but can not find the
proper set of commands. I would like
to transform an xml file (in code behind) and display its output in a
specific html tag, such as a div with
a runat=server. I can somewhat do this if I create a server control
and include the control within the html
div tag but this method (borrowed from ASP.NET Website Programming by
Wrox press thanks guys) does not give me
the full...
|
by: DareDevil |
last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by the user
It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning.
I tested with a simple string field and it alter the string as expected but...
|
by: Ghislain Tanguay |
last post by:
Hi! I'm maybe from old school but, for me, whebn it's time to call a method
from DCOM, remoting or web service returning a
string or anything else I use a function. It's more verbose.
In my new company, much of my coworker works with Public sub ( byval Itemx
as integer, byref returnCode as Integer).
I don't understand why they use this kind of sub and i'm wondering if this
kind of sub take more time to response?
|
by: Timothy |
last post by:
Hi,
I was looking over some differences between C# and VB code today and noticed
that the set method in properties act completely different. C# values are
passed by reference, and VB values are passed by value... Am I right? Why is
there such a big difference in implementation of properties?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |