473,399 Members | 3,603 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,399 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 5031
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.