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

Home Posts Topics Members FAQ

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 10476
jvb
This is from the help file:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2004 JAN.1033/vblr7/html/vaGrpTypeConver sion.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*********@ne tzero.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(<expressi on>, 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(<exp ression>, 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*********@ne tzero.net> schreef in bericht
news:11******** **************@ j33g2000cwa.goo glegroups.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*********@ne tzero.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.WriteLi ne(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
21426
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
5068
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
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...
101
19260
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
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,...
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
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.