Results 1 to 7 of 7

Thread: Converting to string

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

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

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  4. #4

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Converting to string

    I haven't tested performance, but the negative (total failure) outweighs any performance benefit.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting to string

    Quote 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.
    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width