• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2619
  • Last Modified:

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?

Thanks,

Dabas
0
Dabas
Asked:
Dabas
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
amyhxuCommented:
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.
0
 
DabasAuthor Commented:
amyhxu,
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?

Dabas
0
 
S-TwilleyCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RonaldBiemansCommented:
Also not for points :-) The tostring is faster then the legacy cstr function
0
 
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodeleditorbrowsablestateclasstopic.asp

Example:

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

Bob
0
 
DabasAuthor Commented:
Ronald:
> 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?

Dabas
0
 
RonaldBiemansCommented:
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")
        MsgBox(x.ToString)

0
 
DabasAuthor Commented:
RonaldBiemans:
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

Dabas
0
 
S-TwilleyCommented:
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)
0
 
RonaldBiemansCommented:
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 :-))

0
 
DabasAuthor Commented:
Thanks!

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....

Dabas
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now