473,287 Members | 1,827 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,287 software developers and data experts.

.toString() Ctype(), cstr(), DirectCast() ?? Which to use?

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()
myStr = CType(myInt, String)
or use DirectCast()

What is the preferred, fastest way? Any way I shouldn't be using because it
could be deprecated?

Thanks,
--Michael
Nov 20 '05 #1
5 5026
DirectCast won't work for this example - it only works if the run time types
are the same e.g.

------------

Dim o as Object = "Hello"
Dim i as Object = Cint(5)
Dim s as string = directcast(o, String) ' Will work, because o is a string
at runtime
Dim s1 as String = directcast(1, String) ' Won't work

------------

As for the other ways, they are more or less the same. In the examples you
gave, if you follow the IL generated, they you'll notice that all end up
calling the Int32.ToString() method.

See the other posts in this group relating to this issue:

"CType() versus Convert.ToXXXX()"
"DirectCast vc CType"

HTH,

Trev.

"Michael Ramey" <raterus@localhost> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl...
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()
myStr = CType(myInt, String)
or use DirectCast()

What is the preferred, fastest way? Any way I shouldn't be using because it could be deprecated?

Thanks,
--Michael

Nov 20 '05 #2
Michael Ramey wrote:
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()
myStr = CType(myInt, String)
or use DirectCast()

What is the preferred, fastest way? Any way I shouldn't be using
because it could be deprecated?

Thanks,
--Michael


They are are good ways of doing it, but I prefer ToString simply because I
feel it's the most readable.
Pick any of them you like best, and stick with it (for consistancy).

- Pete
Nov 20 '05 #3
AirPete wrote:
Michael Ramey wrote:
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()
myStr = CType(myInt, String)
or use DirectCast()

What is the preferred, fastest way? Any way I shouldn't be using
because it could be deprecated?

Thanks,
--Michael


They are are good ways of doing it, but I prefer ToString simply
because I feel it's the most readable.
Pick any of them you like best, and stick with it (for consistancy).

- Pete


Oops, I didn't see DirectCast, and as Trev noted, it only works if the types
are the same.

- pete
Nov 20 '05 #4
> Oops, I didn't see DirectCast, and as
Trev noted, it only works if the types are the same.


Or if the types are related by inheritance or interfaces - i.e. a cast is
avaialble - not a conversion.

Trev.
Nov 20 '05 #5
Cor
Hi Michael.

For .toString you have only to push on the dot.

Why take another one?

Do not forget that in every program language a lot of things are for
backward compatibility.

Cor
Nov 20 '05 #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.
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
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
5
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
7
by: John | last post by:
Hi I have a WinForm app with a bound form. When user enters a value in field rateid I lookup the respective rate amount from a table and assign it to field rate.I am using the DLookup function...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.