Home All Groups Group Topic Archive Search About

difference between cstr vs .ToString vs Ctype

Author
19 Apr 2006 1:38 PM
c_shah
Very beginner question..

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

Author
19 Apr 2006 1:45 PM
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.
Author
22 Apr 2006 2:48 PM
david
On 2006-04-19, c_shah <shah.chi***@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.
Author
1 May 2006 7:39 PM
c_shah
David, very good explanation

only question I have it if expression is already a string why do you
want to use
DirectCast?
Author
1 May 2006 9:01 PM
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

Show quoteHide quote
"c_shah" <shah.chi***@netzero.net> schreef in bericht
news:1146512351.218629.306130@j33g2000cwa.googlegroups.com...
> David, very good explanation
>
> only question I have it if expression is already a string why do you
> want to use
> DirectCast?
>
Author
2 May 2006 12:17 AM
david
On 2006-05-01, c_shah <shah.chi***@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