473,698 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between Convert.ToStrin g(obj) and CType(obj, String)


Hi--

What is the difference between Convert.ToStrin g(obj) and CType(obj,
String)?

(Assume obj is a variable of type Object.)

Please advise.

Thank you.

--Mark
Nov 19 '05 #1
3 21417
Mark,
If obj is type Object, why not just call obj.ToString()?

Convert.ToStrin g is a framework function. Passing it an Object, causes it to
use the IConvertable interface that the object implements, String implements
this interface. I suspect it ultimately calls obj.ToString()

CType is a VB.NET keyword. It calls some internal VB.NET runtime functions.
I suspect it ultimately calls obj.ToString()

Don't forget there is also CStr, which is effectively shorthand for
CType(obj, String)

Also, for reference types (String is a reference type). if you know obj is a
String, DirectCast is better.

The following articles may help:

Visual Basic .NET Internals
http://msdn.microsoft.com/library/de...tinternals.asp

Performance Optimization in Visual Basic .NET
http://msdn.microsoft.com/library/de...tchPerfOpt.asp

The first article has a section titled: Conversion Functions, CType,
DirectCast, and System.Convert that should address your question.

Hope this helps
Jay
"Mark Kamoski" <mk******@yahoo .com> wrote in message
news:Op******** ******@TK2MSFTN GP10.phx.gbl...

Hi--

What is the difference between Convert.ToStrin g(obj) and CType(obj,
String)?

(Assume obj is a variable of type Object.)

Please advise.

Thank you.

--Mark

Nov 19 '05 #2


Jay--

Which do YOU prefer?

Which do you recommend?

Please advise.

Thank you.

--Mark
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:u1******** ******@TK2MSFTN GP11.phx.gbl...
Mark,
If obj is type Object, why not just call obj.ToString()?

Convert.ToStrin g is a framework function. Passing it an Object, causes it
to
use the IConvertable interface that the object implements, String
implements
this interface. I suspect it ultimately calls obj.ToString()

CType is a VB.NET keyword. It calls some internal VB.NET runtime functions.
I suspect it ultimately calls obj.ToString()

Don't forget there is also CStr, which is effectively shorthand for
CType(obj, String)

Also, for reference types (String is a reference type). if you know obj is
a
String, DirectCast is better.

The following articles may help:

Visual Basic .NET Internals
http://msdn.microsoft.com/library/de...tinternals.asp

Performance Optimization in Visual Basic .NET
http://msdn.microsoft.com/library/de...tchPerfOpt.asp

The first article has a section titled: Conversion Functions, CType,
DirectCast, and System.Convert that should address your question.

Hope this helps
Jay
"Mark Kamoski" <mk******@yahoo .com> wrote in message
news:Op******** ******@TK2MSFTN GP10.phx.gbl...

Hi--

What is the difference between Convert.ToStrin g(obj) and CType(obj,
String)?

(Assume obj is a variable of type Object.)

Please advise.

Thank you.

--Mark


Nov 19 '05 #3
Mark,
I prefer to use the one that is appropriate at the time!

I recommend using the one that is appropriate at the time!

If I were a wood worker: When I needed a pocket knife, I would use a pocket
knife. When I needed a hatchet, I would use a hatchet. When I needed a chain
saw, I would use a chain saw.

In other words, there is no simple answer. There is no magic bullet. You
need to read the articles, learn, formulate ideas.

If you look at my code, when I have any variable and I need a string, I
normally use ToString (sometimes I use String.Format, sometimes other
functions). When I have an object, and I need a specific type, I use
DirectCast. When DirectCast is not appropriate I use CType (enums mostly).
However these are not hard and fast rules, these are applying what you learn
by reading the articles we gave you applying the knowledge contained within.

Hope this helps
Jay

"Mark Kamoski" <mk******@yahoo .com> wrote in message
news:u8******** ******@TK2MSFTN GP12.phx.gbl...


Jay--

Which do YOU prefer?

Which do you recommend?

Please advise.

Thank you.

--Mark
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message news:u1******** ******@TK2MSFTN GP11.phx.gbl...
Mark,
If obj is type Object, why not just call obj.ToString()?

Convert.ToStrin g is a framework function. Passing it an Object, causes it
to
use the IConvertable interface that the object implements, String
implements
this interface. I suspect it ultimately calls obj.ToString()

CType is a VB.NET keyword. It calls some internal VB.NET runtime functions. I suspect it ultimately calls obj.ToString()

Don't forget there is also CStr, which is effectively shorthand for
CType(obj, String)

Also, for reference types (String is a reference type). if you know obj is
a
String, DirectCast is better.

The following articles may help:

Visual Basic .NET Internals
http://msdn.microsoft.com/library/de...tinternals.asp
Performance Optimization in Visual Basic .NET
http://msdn.microsoft.com/library/de...tchPerfOpt.asp
The first article has a section titled: Conversion Functions, CType,
DirectCast, and System.Convert that should address your question.

Hope this helps
Jay
"Mark Kamoski" <mk******@yahoo .com> wrote in message
news:Op******** ******@TK2MSFTN GP10.phx.gbl...

Hi--

What is the difference between Convert.ToStrin g(obj) and CType(obj,
String)?

(Assume obj is a variable of type Object.)

Please advise.

Thank you.

--Mark


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2204
by: Mike Brown | last post by:
This works as expected (this is on an ASCII terminal): >>> unicode('asdf\xff', errors='replace') u'asdf\ufffd' This does not work as I expect it to: >>> class C: .... def __str__(self):
5
2326
by: | last post by:
is it possible to do this. object obj; TextBox tb; obj="textbox"+strA+strB; tb=obj; THANK YOU
0
976
by: | last post by:
thanks Chua Wen Ching + others , I'm sorry I didn't maker my intentions clear. I did try the code but it obviously didnt work thats why I was asking for HELP. basically I was trying to write code for the following. textboxobj= (TextBox) Obj; I have grid of textboxes and need a coomon routine
8
6181
by: Ottar | last post by:
I have a few numeric fields, and when I update i get the error: "Input string was not in a correct format". Next line:" System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2178925" This is beacuse some of my TextBoxes are empty (no value). If I enter a value it is OK. I need to use Null values to indicate that the field is not fileld in.
2
4972
by: Jarod_24 | last post by:
Structure myXMLtype Dim Filename As String Dim sql As String End Structure .... Dim obj as New ArrayList ...... return CType(obj.ToArray, myXMLtype) 'This wont work
2
2237
by: Ronchese | last post by:
Hi all. Is possible I coerce a value to a determined type informed by a string? See the sample: dim obj as Object obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")
5
12720
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
0
1717
by: polygonVB | last post by:
Hi All, I'm trying ot make a error handler function. I want the error handler function to return a command: Resume Resume Next Exit Sub I know how to return it as a sting but I don't now how to convert that string to a command. I was trying something like: ctype(Function(Err), Command)
7
1672
by: Ray Proffitt | last post by:
Hi. obj.ToString(); or obj + ""; Which is more efficient and why?
0
8609
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,...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
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
8871
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...
1
6525
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
5861
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.