tags:

views:

509

answers:

5

I am wondering which is more efficient, using CStr() or object.toString(). The reason I ask this is because I though all that CStr() done was to invoke the .ToString() method on the object it was dealing with.

But when recently using a generic method without any type constraints I had to use object.ToString() instead of CStr(object), the following is purely an example to illustrate the issue.

Public Function IDFromObject(Of ID_TYPE)(ByVal value As ID_TYPE) As String
    Return value.ToString
End Function

Compiled as expected, but the following did not using CStr(). It gave an compilation error value of type ID_TYPE cannot be converted to string. But it obviously can using .ToString()

Public Function IDFromObject(Of ID_TYPE)(ByVal value As ID_TYPE) As String
    Return CStr(value)
End Function
+3  A: 

From here (couldn't say it any better):

CStr is a keyword, whereas ToString is a function (method). CStr is compiled inline and it creates code depending on the type of the passed object. It's mainly there for people being used to it from previous VB versions. I haven't used CStr in .Net anymore (because it's not obvious what it does in which situations and it's also not very well documented).

The difference depends on which ToString function you use. Every type can have it's own implementation.

fretje
A: 

CStr(object) is a cast (equivalent to (string)object in C#) and will throw esxception if given a null obejct or an object that cannot be casted to string. However .ToString() will work on any type of object (since it implemented in the Object class) and if not overridden by the current class will return the base ToString() method. In your case you must override ToString() method in your ID_TYPE class and return the string you need.

AlexDrenea
Obviously, calling ToString() on a null object will not work. So in that respect, it is the same as CStr :)
Thorarin
A: 

Here is an MSDN link on the Type Conversion Functions which you might find useful.

kevchadders
A: 

They are two completely different things, CStr is an overloaded function that converts the data within certain types into a string while ToString calls a method that all .net objects have and which you can override but which by default contains the name of the object. ToString will only return the data of a type if it has been overridden to do so.

Patrick
A: 

For readability sake, if I were in the need to write code in VB.NET (I am a C# programmer), I would avoid the VB specific keywords/functions as much as possible. By using .NET classes and methods only, your code will be more understandable by people used to develop in other .NET languages. Not to mention that these functions are there mainly for compatibility with VB6 and they look a bit out of place compared to the .NET way of doing things.

Of course there may be reasonable exceptions, sometimes VB.NET really makes very easy to do certain tasks and it can be convenient to take advantage of this; but as a genereal rule I would not use any VB.NET specific function.

Konamiman