how to parse a text box to an integer - but....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: how to parse a text box to an integer - but....

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    how to parse a text box to an integer - but....

    If you have a form, with multiple text boxes. R1_fred, R2_fred, R3_fred -- and would like to parse what is in that text box into an integer, you could use:

    Code:
    risk = int.Parse(R1_fred.Text);

    However, I'd like to replace "R1_" with "R2_" or "R3_". Unfortunately, I cannot do the following:


    Code:
    string rowNumber = "R1_"
    
    string temp1 = "fred.Text";
    string temp2 = temp1.Insert (0, rowNumber);
    risk = int.Parse(temp2);
    and expect it to work. What code could I use to effectively do the same thing?




    Please be gentle, I am somewhat new to C#.

    Thanks!



    p.s. using .net 4.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: how to parse a text box to an integer - but....

    Hello! Your solution of trying to store the variable name in a string made me smile. That seems like a very Bash (linux command line) way of thinking. Clever! But, not correct unfortunately!

    Anyway, you can use an array:

    Code:
    TextBox[] textArray = new TextArray[] { R1_fred, R2_fred, R3_fred };
    int risk = int.Parse(textArray[1].Text); //Will parse the value in R2_fred (at index 1 of the array; arrays always count from zero).
    Or, better, a List<T>:

    Code:
    List<TextBox> textArray = new List<TextBox>() { R1_fred, R2_fred, R3_fred };
    int risk = int.Parse(textArray[1].Text); //Will parse the value in R2_fred (at index 1 of the array; arrays always count from zero).
    The list code uses generics, which are very important. You should learn about them, if you haven't already: http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    Also, when parsing input from users, it's best to expect errors. The TryParse method allows for this. You call it like this:

    Code:
    int risk; //Declare a variable to store the value
    if( !Int32.TryParse(textBox.Text, out risk) )
    {
        //Error handling here
    }
    Here the tryparse method returns a bool: true if the parsing was successful, otherwise false. If parsing was successful, the value is stored in the risk variable (which you passed as an out parameter). If parsing isn't successful, the risk variable has some undefined value (but you're going to handle the error anyway, so you can make some intelligent choice).

    Hope that helps!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jan 2013
    Posts
    2

    Re: how to parse a text box to an integer - but....

    Just wanted to say thanks for the help. Your response prompted me to look more into arrays. And.. After figuring them out and applying them what I was doing, it turns out I didn't need to do any of the things my original post asked about.

    Thanks again. I learned quite a few things from your post.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: how to parse a text box to an integer - but....

    Great! Glad it helped!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    51

    Re: how to parse a text box to an integer - but....

    Quote Originally Posted by BioPhysEngr View Post
    Hello! Your solution of trying to store the variable name in a string made me smile. That seems like a very Bash (linux command line) way of thinking. Clever! But, not correct unfortunately!

    Anyway, you can use an array:

    Code:
    TextBox[] textArray = new TextArray[] { R1_fred, R2_fred, R3_fred };
    int risk = int.Parse(textArray[1].Text); //Will parse the value in R2_fred (at index 1 of the array; arrays always count from zero).
    Or, better, a List<T>:

    Code:
    List<TextBox> textArray = new List<TextBox>() { R1_fred, R2_fred, R3_fred };
    int risk = int.Parse(textArray[1].Text); //Will parse the value in R2_fred (at index 1 of the array; arrays always count from zero).
    The list code uses generics, which are very important. You should learn about them, if you haven't already: http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    Also, when parsing input from users, it's best to expect errors. The TryParse method allows for this. You call it like this:

    Code:
    int risk; //Declare a variable to store the value
    if( !Int32.TryParse(textBox.Text, out risk) )
    {
        //Error handling here
    }
    Here the tryparse method returns a bool: true if the parsing was successful, otherwise false. If parsing was successful, the value is stored in the risk variable (which you passed as an out parameter). If parsing isn't successful, the risk variable has some undefined value (but you're going to handle the error anyway, so you can make some intelligent choice).

    Hope that helps!
    I don't see the reasoning behind using a List<T> here unless he is going to add more controls dynamically to this list that he wants to use to replace R1_ with... That is unnecessary. For textbox input though, you should always use TryParse().

    spinn, how are you deciding whether R2_ or R3_ replaces R1_? At random or?

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: how to parse a text box to an integer - but....

    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    51

    Re: how to parse a text box to an integer - but....

    That whole article is based on the rant for why Eric doesn't need arrays in his programming venture's for the most part, and under specific circumstances. It doesn't mean that they should never be used because they do offer performance gains over other collection types, when the functionality of one over the other is not required by the design of the application itself.

    "Parallelism Problems" and:
    Arrays make optimizing for things like “swapping two values” easy, but destroy the larger ability to optimize for parallelism.
    However, we're not working with Parallel anything here as far as I know...

    Jon Skeet said:
    Really just answering to add a link which I'm surprised hasn't been mentioned yet: Eric's Lippert's blog entry on "Arrays considered somewhat harmful."

    You can judge from the title that it's suggesting using collections wherever practical - but as Marc rightly points out, there are plenty of places where an array really is the only practical solution.
    As I said though, we are not needing to resize this array, so no expense there. In fact, we don't even have to use an array or a collection for this if it's only these 3 controls, why would you?

    Here was a reasonable guide I seen on the differences:
    Use an array when you are dealing with data that is:

    • fixed in size, or unlikely to grow much
    • suitably large (more than 10, 50, 100 elements, depending on the algorithm)
    • you will be doing lots of indexing into it, i.e. you know you will often want the third element, or the fifth, or whatever.

    Use a list for:

    • variable length data lists
    • that are mostly used as a stack or a queue or need to be iterated in its entirety
    • when you do not want to write an expression to derive the ultimate array size for the declaration and you do not want to wastefully pick a large number
    The performance of an array is good. Here's a comment:

    That said, I make a lot of use of arrays in my protobuf-net project; entirely for performance
    List<T> here offers a lot more functionality that we don't even need, there's no need for it. He's cautioning not to use an array because he believes most beginner programmers (what the topic was instigated from), would not understand how they should be used effectively, and a collection based type would help make things easier on that front.
    Last edited by AceInfinity; February 3rd, 2013 at 05:21 PM.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center