|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
difference between cstr vs .ToString vs CtypeVery beginner question..
What's difference between cstr vs .ToString vs Ctype for converting to String? 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. On 2006-04-19, c_shah <shah.chi***@netzero.net> wrote:
> Very beginner question.. You missed the other option, which is DirectCast.> > What's difference between cstr vs .ToString vs Ctype for converting to > String? 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. David, very good explanation
only question I have it if expression is already a string why do you want to use DirectCast? 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? > On 2006-05-01, c_shah <shah.chi***@netzero.net> wrote:
> David, very good explanation That's for when *you* know it's a string but the compiler doesn't, so> > only question I have it if expression is already a string why do you > want to use > DirectCast? 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
Rnd() vs. Long ...no joy with LSB!
FTP with SSL How to call IE to open a new page. webbrowser System.IO.FileNotFoundException SQL Connection help needed making 600 dpi A3 size images how to move a form that FormBorderStyle is None button with drop down how to extract icon (or resource) ? Printing rotated text |
|||||||||||||||||||||||