473,808 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CStr() vs. .ToString()

Book I am reading says that Cstr() is best method for efficency and safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks
Feb 18 '06
101 19260
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(aSomethin g, String)
' alternatively
aString = CStr(aSomething )

End Sub

In my example I have defined conversions from Something to String to simply
be Something.ToStr ing, 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(ByVa l value As String) As
Something

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Scott M." <s-***@nospam.nosp am> wrote in message
news:e$******** ******@TK2MSFTN GP09.phx.gbl...
| 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.Visua lBasic
| 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?
|
|
|
| "CMM" <cm*@nospam.com > wrote in message
| news:uk******** ******@TK2MSFTN GP12.phx.gbl...
| > 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.Visua lBasic. So maybe it might help to teach your students
what
| > Imports means or have them always type out Microsoft.Visua lBasic.Left or
| > Microsoft.Visua lBasic.MsgBox etc etc.
| >
| > --
| > -C. Moya
| > www.cmoya.com
| > "Scott M." <s-***@nospam.nosp am> wrote in message
| > news:eQ******** ******@TK2MSFTN GP10.phx.gbl...
| >> Your question boils down to this one: What object is CStr() a method
of?
| >>
| >>
| >>
| >> "Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
| >> news:eZ******** ******@TK2MSFTN GP09.phx.gbl...
| >>> 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
| >>>
| >>>
| >>
| >>
| >
| >
|
|
Feb 21 '06 #101
> By instance IsDate what is nothing more than a convert in a try block,
however much easier to write


Cor,

This was the case in the 1.x implementation. However, in 2.0, IsDate wraps
TryParse and avoids the exceptions. (from the IL:
L_001c: call bool Microsoft.Visua lBasic.Compiler Services.Conver sions::TryParse Date(string,
[mscorlib]System.DateTime &)

In this case, TryParse is faster as IsDate just throws away the result of
the cast. If you directly use TryParse and continue working with the result,
your performance improves. (see http://devauthority.com/blogs/jwoole...02/15/747.aspx)

Jim Wooley
Feb 22 '06 #102

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7710
by: Bill L. | last post by:
We recently noticed that the vb.net CStr function yields different results than the vb6 version when converting SQL decimal data. If, for example, the data is defined in SQL as decimal(19,10), the vb.net CStr function will return ten digits to the right of the decimal, regardless of the data value. In this case, if the data value is 5, the vb.net CStr function will return 5.0000000000. The vb6 CStr function will simply return 5. In other...
5
10476
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
7
2770
by: John | last post by:
Hi I have a WinForm app with a bound form. When user enters a value in field rateid I lookup the respective rate amount from a table and assign it to field rate.I am using the DLookup function to achieve this (full code given below). This function returns a value of type object so I need to convert it to string. If I use the function as below (using ToString) it works fine; Me.txtRate.Text = DLookup(<parameters here>).ToString
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
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...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
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...
0
6880
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();...
0
5547
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...
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.