472,780 Members | 1,192 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,780 software developers and data experts.

difference between Convert.ToString(obj) and CType(obj, String)


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.

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

Convert.ToString 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**************@TK2MSFTNGP10.phx.gbl...

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.

--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********@email.msn.com> wrote in message
news:u1**************@TK2MSFTNGP11.phx.gbl...
Mark,
If obj is type Object, why not just call obj.ToString()?

Convert.ToString 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**************@TK2MSFTNGP10.phx.gbl...

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.

--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**************@TK2MSFTNGP12.phx.gbl...


Jay--

Which do YOU prefer?

Which do you recommend?

Please advise.

Thank you.

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

Convert.ToString 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**************@TK2MSFTNGP10.phx.gbl...

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.

--Mark


Nov 19 '05 #4

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

Similar topics

6
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
by: | last post by:
is it possible to do this. object obj; TextBox tb; obj="textbox"+strA+strB; tb=obj; THANK YOU
0
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...
8
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&...
2
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
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
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
0
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...
7
by: Ray Proffitt | last post by:
Hi. obj.ToString(); or obj + ""; Which is more efficient and why?
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
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=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
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...

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.