CStr(Object) vs Object.ToString

I use Option Strict On which often forces me to convert objects into strings.

I have two options to do so:

Using CStr as in CStr(myVariable) or using .ToString as in myVariable.ToString

The second seems to be logically the one to use, but very often .ToString does not appear in intellisense, which makes me doubt it is the right practice.

Which option is better, quicker, or is it a case of 6 vs half a dozen?


LVL 27
Who is Participating?
Yes Dabas, but if firstname would contain a binary large object, I don't think you would like the tostring result :-)

It is just MS being overprotective (it has to account for all fools to, not just the people who know what they are doing :-))

ToString is available for a wide range of objects.
CStr is a type conversion function which converts other data type to string data type.
e.g. ComboBox1.ToString gives you this result: "System.Windows.Forms.ComboBox, Items.Count: 6"
      But you can't use CStr(ComboBox1) which is illegal.
But for data type conversion, they both get the same result.
DabasAuthor Commented:
Good point.
From what you are saying, .ToString probably will never cause an exception, while CStr can.
Also, if it is for data type conversion, and as you say they give the same result, my question is: Is one of the systems BETTER than the other?
For example: .toString is an overriden method already belonging to the object, while CStr might have more overhead, as other objects are involved. On the other hand, why are there so many cases where .ToString does not appear in intellisense?

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

My guess for why .ToString doesn't appear is that the object in question has hidden the .ToString function because there is no sensible string equivalent... but that's all it is, a guess :P

As for CStr... it's probably been left in .NET  like many functions, for the old VB programmers

Well that's my little contribution to this lil discussion, not worth any points though :P... so don't worry about it
Also not for points :-) The tostring is faster then the legacy cstr function
Bob LearnedCommented:
Just because something doesn't appear in Intellisense doesn't mean that it is not accessible.

There is an attribute that can control visibility:

EditorBrowsableState Enumeration:


Control.CanFocus definition as viewed by Reflector:
 <SRDescription("ControlCanFocusDescr"), Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), SRCategory("CatFocus")> _
      Public ReadOnly Property CanFocus As Boolean

DabasAuthor Commented:
> The tostring is faster then the legacy cstr function
Thanks! That is the kind of answer I was looking for!
I also was under the impression that being an object "primitive", .ToString should be faster.

And thanks, Bob, for the intellisense and visibility reference. Question is why was that visibility removed if it is supposed to be faster and replaces a legacy function?

The main reason I think is the fact that for an object the tostring property is usually pretty pointless, because in most cases it won't return something usefull, only if there is actual a string in there and than you would usually use a string object.

even something like this will return a string (system.drawing.bitmap) but what use i that ?

        Dim x As Object
        x = Image.FromFile("c:\1.bmp")

DabasAuthor Commented:
True, but if I have something like

Dim dr as DataRow = ds.Tables(0).Rows(0)
txtFirstName = dr.Item("FirstName").Intellisense_does_not_show_ToString_here

It would be nice if the system would help me instead of forcing me to type .ToString

Maybe it's case of them trying to prevent you from using ".ToString" in some cases where the results might not be what you expected and/or trying to prevent you from writing code which depends on the string equivalent (kinda like how they tell you not to work on the numeric equivalent of boolean values as you might get unexpected results... well it says something like that in the help file)
DabasAuthor Commented:

Yes, it seems to be a case of MS being overprotective.
I would prefer if they would make the .ToString visible and let me learn the hard way if I am converting a BLOB to string....

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.