472,958 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 5001
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.