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
ToString method displays a blank line but output using ToString method throws an un-handled exception. Convert. ToString handles null while ToString doesn't and throws a NULL reference exception.
The CStr() function takes a numeric value and converts it to a string data type. The equivalent in . NET is the Convert. ToString() method. You may also use the ToString() method on the numeric data type.
CSTR in VBA is a data type conversion function which is used to convert any value provided to this function to string, even if the given input is in integer or float value this function will convert the data type of the value to a string data type, so the return type of this function is a string.
Late answer, but no one else bothered to check or cite the Visual Basic documentation, and it recommends the opposite of what most of these answers do. This is straight from the Microsoft documentation:
As a rule, you should use the Visual Basic type conversion functions in preference to the .NET Framework methods such as ToString(), either on the Convert class or on an individual type structure or class. The Visual Basic functions are designed for optimal interaction with Visual Basic code, and they also make your source code shorter and easier to read.
That said, sometimes there are differences between what CStr
and ToString
will return, so performance and readability aren't the only considerations. There is an excellent answer on another question that describes when and why this is the case. Reproduced here:
The ToString method is a standard public method that returns a String. It is a method that is defined by the base Object type as overrideable. Each class can, therefore, override that method to return anything that it wants. It is quite common for classes to override the ToString method to make it return a nice human-readable description of the object.
CStr, on the other hand, is a casting operator. It is shorthand for CType(x, String). The casting operator, like many other operators, can be overridden by any class. Normally, though, you want casting operations to return the closest representation of the actual value of the original object, rather than a descriptive string.
It is not unusual then, that you may want ToString to return a different result than CStr. In the case of an enum, each member is essentially an Integer, so CStr on an enum member works the same as CStr on an integer. That is what you would expect. However, ToString has been overridden to return the more human readable version of the value. That is also what you would expect.
When you want the closest string representation of the actual value of the original object, you should use CStr()
. When you want the (potentially) customized, human-readable version of the value, you should use .ToString
.
One BIG difference between CStr as ToString is handling of Enum variables.
CStr returns the underlying number e.g. "2" and ToString returns the Enum name e.g. "LeftToRight"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With