Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > difference between Convert.ToString(obj) and CType(obj, String)

Reply
Thread Tools

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

 
 
Mark Kamoski
Guest
Posts: n/a
 
      07-23-2003

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


 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      07-23-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
>
>



 
Reply With Quote
 
 
 
 
Mark Kamoski
Guest
Posts: n/a
 
      08-07-2003


Jay--

Which do YOU prefer?

Which do you recommend?

Please advise.

Thank you.

--Mark


"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
>
>




 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      08-08-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> Jay--
>
> Which do YOU prefer?
>
> Which do you recommend?
>
> Please advise.
>
> Thank you.
>
> --Mark
>
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in

message
> news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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
> >
> >

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments