473,656 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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 5057
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@localh ost> wrote in message
news:eV******** ******@TK2MSFTN GP09.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
21415
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
10825
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 found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
7
2711
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
19194
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
10458
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
7
2763
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 to achieve this (full code given below). This function returns a value of type object so I need to convert it to string. If I use the function as below (using ToString) it works fine; Me.txtRate.Text = DLookup(<parameters here>).ToString
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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
8498
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
7311
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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
4150
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1600
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.