difference between cstr vs .ToString vs Ctype

C

c_shah

Very beginner question..

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

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.
 
D

david

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.
 
C

c_shah

David, very good explanation

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

Cor Ligthert [MVP]

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
 
D

david

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top