Results 1 to 9 of 9

Thread: Cstr()

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Question Cstr()

    The Cstr() is not supported in C#.NET. So how would i go about converting a a string to an integer. I.e a number in a text box to an integer.

    regards

    Carl

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Cstr()

    Convert.ToInt32(TextBox.Text.Trim);

    Edit: C# guys and their crazy crazy case sensitivity.

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

    Re: Cstr()

    By the way, CStr, CInt, etc. should not really be used to "convert" anything in VB.NET either. CStr and it's ilk should be used for casting, not converting. "Casting" means that you are changing the type of the reference you are using to access the object, but not changing the object itself. "Converting" means that you are actully changing the object. Here's a VB.NET example of when it would be appropriate to use CStr:
    VB Code:
    1. Dim myArrayList As New ArrayList
    2.  
    3. myArrayList.Add("Some String")
    4.  
    5. Dim myString As String = CStr(myArrayList(0))
    The object in the ArrayList IS a String but indexing the ArrayList retruns an Object reference, so you cast it as a String. At no time is the object itself anything other than a String.

    On another note, if you're converting the contents of a TextBox to an Int32 then you'd better be sure that the String actually does represent a valid integer or else Convert.ToInt32 or Int32.Parse will both throw an exception. If you're converting user input you really should validate it, unless you've already done so by handling keyboard events, etc. It is generally a good idea to use a TryParse method when converting user inputted text to numbers. If you're using .NET 1.x you only have Double.TryParse, but you can use the arguments to specify that only integers are valid. If you're using .NET 2.0 you have Int32.TryParse as well.
    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

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Cstr()

    I had heard somewhere that TryParse throws an exception behind the curtains. Is that true?

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

    Re: Cstr()

    Quote Originally Posted by sevenhalo
    I had heard somewhere that TryParse throws an exception behind the curtains. Is that true?
    Absolutely not. In fact, TryParse was created specifically to avoid an exception being thrown. Here's the story I've read, although I can't guarantee that it's true.

    Microsoft created the IsDate and IsNumeric Runtime functions to mimic the equivalent functions in VB6. IsDate merely tried to convert the specified object to a Date and caught the exception if it failed. That much is true. It is pretty terrible programming practice but it's true. IsNumeric was implemented the same way for numbers. The developers at Microsoft realised that this produced pretty terrible performance if the objects failed to convert, thus they created Double.TryParse. IsNumeric calls Double.TryParse internally to avoid an exception being thrown when the conversion failed. Now that Date.TryParse exists in .NET 2.0, I'd imagine that IsDate has had it's implementation changed so that it too will avoid throwing exceptions. I guess they figured that IsNumeric would get used more often than IsDate so it was more important to get working properly.
    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
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Question Re: Cstr()

    Thanks for all of your help. I'm trying to make a program that adds to numbers in the two fileds but the Val() function does not seem to work, not supported in C# either. i need a way for it to recognise the text bos string as an integer so i though i would try and convert it. Am i on the right line or am i way off?

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

    Re: Cstr()

    Absolutely, but no VB6-style functions are going to work in C#. A lot of functions were added to the VB.NET run-time library that mimic functions that existed in VB6. These are specifically so that VB6-style code won't break in VB.NET, but C# has no such concern. In actual fact, the VB.NET run-time library is just a .NET assembly, so you can actually reference it from a C# app, but I strongly recommend against doing so. Val works in a very dodgy way anyway, and exemplifies for me the sort of thing that is responsible fro VB having been looked down upon by other developers.

    Anyway, what you should do in C#, and VB.NET for that matter, is use a TryParse method. If you are using 2005 you can use Integer.TryParse. If you're using 2003 you have to use Double.TryParse, but you can specify that only integers are valid. Look them up in the help/MSDN. The help topics will have code examples.
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: Cstr()

    Thanks you very much for your help

  9. #9
    Addicted Member MasterBlaster's Avatar
    Join Date
    Jul 2002
    Location
    Seattle
    Posts
    196

    Re: Cstr()

    textboxes fire validation events. you might want to think about using them.
    Code:
    this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
    Code:
    public bool IsInteger(string strNumber)
        {
          Regex rexNotInt=new Regex("[^0-9-]");
          Regex rexInt=new Regex("^-[0-9]+$|^[0-9]+$");
    
          return  !rexNotInt.IsMatch(strNumber) &&
                  rexInt.IsMatch(strNumber);
        }
    "And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
    Frank Zappa

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