J
Jay B. Harlow [MVP - Outlook]
Scott,
| My question still stands... What object is CStr() a method of?
Ultimately: Its a method of the object that you pass as a parameter.
As has been pointed out CStr is shorthand for CType(value, String).
In VS 2005 you can overload CType for individual types. Which means you
could define a conversion from a type to String, such as:
Public Class Something
Public Shared Narrowing Operator CType(ByVal value As Something) As
String
Return value.ToString()
End Operator
End Class
Then you could use CStr to convert from that type to a String.
Public Sub Main(ByVal args As String())
Dim aSomething As New Something
Dim aString As String = CType(aSomething, String)
' alternatively
aString = CStr(aSomething)
End Sub
In my example I have defined conversions from Something to String to simply
be Something.ToString, however! that is *NOT* a requirement. Conversion to a
string may in fact be a different operation then the ToString method!
(although I would carefully evaluate types where ToString & CStr are defined
differently). More importantly ToString can be defined on a type where CStr
is not. For example ToString may return a human readable format for use in
List Boxes, where as CStr may return a string of hex digits...
Generally I would only define CStr (as above) where it is common to convert
to & from the type & a String. In which case I would also define:
Public Shared Narrowing Operator CType(ByVal value As String) As
Something
Public Shared Function FromString(ByVal value As String) As
Something
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
| Please don't drag in unrelated issues into this conversation. I fully
| understand what Imports and Namespaces are and my students learn them.
Your
| point that these are language elements declared in the
Microsoft.VisualBasic
| don't change any of what I've been saying about my preference not to use
| CStr(). Did you really think I'd fall into the camp that wants to use
| Left/Right and MsgBox instead of the MessageBox class and the other String
| object methods anyway?
|
| My question still stands... What object is CStr() a method of?
|
|
|
| | > CStr is compiled as an inline conversion.... similarly to C#'s AND C++'s
| > (and probably Java's)
| > s2 = (string) object
| > gasp! that's not OO!!!!
| >
| > On the other hand, Left/Right/Mid/etc etc are function in
| > Microsoft.VisualBasic. So maybe it might help to teach your students
what
| > Imports means or have them always type out Microsoft.VisualBasic.Left or
| > Microsoft.VisualBasic.MsgBox etc etc.
| >
| > --
| > -C. Moya
| > www.cmoya.com
| > | >> Your question boils down to this one: What object is CStr() a method
of?
| >>
| >>
| >>
| >> | >>> Scott,
| >>>
| >>> What is this for you?
| >>>
| >>> \\\
| >>> Imports ScottS.Cmm
| >>> Class WhatEver
| >>> Private Sub WhatEver()
| >>> Dim i As Integer = 1
| >>> Dim a As String = CIS(i)
| >>> End Sub
| >>> End Class
| >>> ///
| >>> In a different DLL
| >>> \\\
| >>> Public Class Cmm
| >>> Public Shared Function CIS(ByVal i As Integer) As String
| >>> Return i.ToString
| >>> End Function
| >>> End Class
| >>> ///
| >>>
| >>> Cor
| >>>
| >>>
| >>
| >>
| >
| >
|
|
| My question still stands... What object is CStr() a method of?
Ultimately: Its a method of the object that you pass as a parameter.
As has been pointed out CStr is shorthand for CType(value, String).
In VS 2005 you can overload CType for individual types. Which means you
could define a conversion from a type to String, such as:
Public Class Something
Public Shared Narrowing Operator CType(ByVal value As Something) As
String
Return value.ToString()
End Operator
End Class
Then you could use CStr to convert from that type to a String.
Public Sub Main(ByVal args As String())
Dim aSomething As New Something
Dim aString As String = CType(aSomething, String)
' alternatively
aString = CStr(aSomething)
End Sub
In my example I have defined conversions from Something to String to simply
be Something.ToString, however! that is *NOT* a requirement. Conversion to a
string may in fact be a different operation then the ToString method!
(although I would carefully evaluate types where ToString & CStr are defined
differently). More importantly ToString can be defined on a type where CStr
is not. For example ToString may return a human readable format for use in
List Boxes, where as CStr may return a string of hex digits...
Generally I would only define CStr (as above) where it is common to convert
to & from the type & a String. In which case I would also define:
Public Shared Narrowing Operator CType(ByVal value As String) As
Something
Public Shared Function FromString(ByVal value As String) As
Something
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
| Please don't drag in unrelated issues into this conversation. I fully
| understand what Imports and Namespaces are and my students learn them.
Your
| point that these are language elements declared in the
Microsoft.VisualBasic
| don't change any of what I've been saying about my preference not to use
| CStr(). Did you really think I'd fall into the camp that wants to use
| Left/Right and MsgBox instead of the MessageBox class and the other String
| object methods anyway?
|
| My question still stands... What object is CStr() a method of?
|
|
|
| | > CStr is compiled as an inline conversion.... similarly to C#'s AND C++'s
| > (and probably Java's)
| > s2 = (string) object
| > gasp! that's not OO!!!!
| >
| > On the other hand, Left/Right/Mid/etc etc are function in
| > Microsoft.VisualBasic. So maybe it might help to teach your students
what
| > Imports means or have them always type out Microsoft.VisualBasic.Left or
| > Microsoft.VisualBasic.MsgBox etc etc.
| >
| > --
| > -C. Moya
| > www.cmoya.com
| > | >> Your question boils down to this one: What object is CStr() a method
of?
| >>
| >>
| >>
| >> | >>> Scott,
| >>>
| >>> What is this for you?
| >>>
| >>> \\\
| >>> Imports ScottS.Cmm
| >>> Class WhatEver
| >>> Private Sub WhatEver()
| >>> Dim i As Integer = 1
| >>> Dim a As String = CIS(i)
| >>> End Sub
| >>> End Class
| >>> ///
| >>> In a different DLL
| >>> \\\
| >>> Public Class Cmm
| >>> Public Shared Function CIS(ByVal i As Integer) As String
| >>> Return i.ToString
| >>> End Function
| >>> End Class
| >>> ///
| >>>
| >>> Cor
| >>>
| >>>
| >>
| >>
| >
| >
|
|