473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

difference between cstr vs .ToString vs Ctype

Very beginner question..

What's difference between cstr vs .ToString vs Ctype for converting to
String?

Apr 19 '06 #1
5 10404
jvb
This is from the help file:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2004JAN.1033/vblr7/html/vaGrpTypeConversion.htm

To sum it up, CStr is faster since there is no outside method call.

Apr 19 '06 #2
On 2006-04-19, c_shah <sh*********@netzero.net> wrote:
Very beginner question..

What's difference between cstr vs .ToString vs Ctype for converting to
String?


You missed the other option, which is DirectCast.

This isn't a simple question, because which to choose often depends on
what kind of object you want to convert and where you got that object.

..ToString will call the .ToString() function on a particular instance.
In practice, this means that it will throw an exception if the object in
question is Nothing. However, you can implement .ToString() in your own
classes to get a useful string representation of your object, whereas
CType/CStr only work with built-in classes and interfaces.

CStr and CType(<expression>, String) are exactly equivalent (I'm not
sure where the other poster got the idea that CStr is faster). But they
aren't really functions, they're compiler directives that will emit very
different code depending on the declaration of <expression>. In most
cases, these directives call a bunch of internal VB code that tries to
get a reasonable string out of <expression>.

DirectCast(<expression>, String) assumes that the expression in
question really is a string and just casts it. It's the fastest of all
these options, but will throw an exception if <expression> is anything
other than a string.

Which to choose depends on what you want to do.

Apr 22 '06 #3
David, very good explanation

only question I have it if expression is already a string why do you
want to use
DirectCast?

May 1 '06 #4
CO-Shah,

ToString is a method from Object and therefore it is in every class.
Mostly it is overridden and gives than a wanted format
The overloaded format from by instance date is very extended.

If it is not overridden if gives I thought the classname.

CStr is a convert function to set values to a string probably will
internally be used the standard toString method, I did not check it.

CType is the general convert function (where CStr is specialized). It is to
Convert one Type to another Type.

DirectCast is to Cast (use) an object from a type which derives or using the
same parent or interfaces as the casted type.

I hope this gives an idea

Cor

"c_shah" <sh*********@netzero.net> schreef in bericht
news:11**********************@j33g2000cwa.googlegr oups.com...
David, very good explanation

only question I have it if expression is already a string why do you
want to use
DirectCast?

May 1 '06 #5
On 2006-05-01, c_shah <sh*********@netzero.net> wrote:
David, very good explanation

only question I have it if expression is already a string why do you
want to use
DirectCast?


That's for when *you* know it's a string but the compiler doesn't, so
you DirectCast it into a string variable. Simplest example..

Private Sub Foo(o as Object)

If Typeof(o) Is String Then
Dim s As String = DirectCast(o, String)
Console.WriteLine(s.Length)

......

After the TypeOf, the object has to be a string, but you still need to
cast it to assign it to a string variable, otherwise the compiler
complains

May 2 '06 #6

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

Similar topics

3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
9
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...
101
by: Sean | last post by:
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
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.