|
-
May 2nd, 2008, 03:17 PM
#1
Converting to string
Here's a general question. I have some code that was generated via templates. In general, wherever an object needed to be turned into a string, the templates used CStr(). This works fine for nearly everything...until I switched a field to a GUID, at which point CStr() throws an exception. Using .ToString works fine for GUIDs, and I can't think of any situation where .ToString will fail when CStr() would work. All of the items that are being passed in are fields in a datarow, datagridviewrow, pure objects (that are generally fields from datarows), or something like that. Am I overlooking a situation?
My usual boring signature: Nothing
 
-
May 2nd, 2008, 03:24 PM
#2
Re: Converting to string
Everything in .NET inherits from System.Object, so everything in .NET has the ToString method available to it, therefore it should never fail on you except if the object isn't set to anything and it throws the null reference exception because you're trying to call a method of it. CStr is a holdover from classic VB and, while it works in most cases, isn't really part of the .NET Framework and it isn't the best practice to use it instead of ToString.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
May 2nd, 2008, 03:24 PM
#3
Re: Converting to string
I don't think the .NET objects will ever throw an exception while calling .ToString. Also, the situation you described will also occur when converting dbnull types, rather than calling ToString. I beleive the positive for converting rather than .ToString is performance.
-
May 2nd, 2008, 03:29 PM
#4
Re: Converting to string
I haven't tested performance, but the negative (total failure) outweighs any performance benefit.
My usual boring signature: Nothing
 
-
May 3rd, 2008, 01:50 AM
#5
Re: Converting to string
 Originally Posted by Tom Sawyer
Everything in .NET inherits from System.Object, so everything in .NET has the ToString method available to it, therefore it should never fail on you except if the object isn't set to anything and it throws the null reference exception because you're trying to call a method of it.
Correct.
 Originally Posted by Tom Sawyer
CStr is a holdover from classic VB and, while it works in most cases, isn't really part of the .NET Framework and it isn't the best practice to use it instead of ToString.
Not true at all. CStr is no more a holdover from VB6 than any other VB.NET keyword. It is NOT a Runtime function (a member of the Microsoft.VisualBasic namespace) like MsgBox or Len. It is part of the VB.NET language itself, hence it turns blue in the code window. The truth of the matter is that CStr(obj) is simply a shorthand for CType(obj, String).
It is NOT best practice to use ToString in preference to CStr. CStr is more efficient than ToString because it is compiled inline rather than as a function call. If you want to cast a reference to a String object as type String then CStr is preferable to ToString. If you want to convert an object for which the CType operator is defined for type String then CStr is preferable to ToString. If you want to convert an object that is not a String, and for which the CType operator is not defined for type String, to a String representation then ToString is the only option.
-
May 6th, 2008, 11:25 AM
#6
Re: Converting to string
This is generated code, so the type of the object is not known so well. After a bit of discussion we realized that no approach was correct for all cases. If you write a function that turns an object into a string, there are cases where either CStr or .ToString will raise exceptions. Therefore, we will be writing something that will handle the correct types the correct ways to the extent that that is possible for template based programs.
My usual boring signature: Nothing
 
-
May 6th, 2008, 05:22 PM
#7
Re: Converting to string
If the type is not known the I think all you'd have to do is test for a null reference. Use CStr if it is and ToString otherwise. I can't think of a situation where that wouldn't work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|