Simple If Statement Question

Is there a better way to avoid an exception than the following If statement   I have a button that invokes this code.  I don't want an error to occur if they leave the txtPennies.Text blank or with some other unexpected input.  I made the If statement say if the text was blank, just return, but what if I wanted the If statement to go though all of the lines of code

 

{

int intLeftOver;

if (txtPennies.Text == ""//Is there a better way

return;

intLeftOver = int.Parse(txtPennies.Text);

lblDollars.Text = (intLeftOver / 100).ToString ();

intLeftOver = intLeftOver % 100;

lblQuarters.Text = (intLeftOver / 25).ToString();

intLeftOver = intLeftOver % 25;

lblDimes.Text = (intLeftOver / 10).ToString();

intLeftOver = intLeftOver % 10;

lblNickels.Text = (intLeftOver / 5).ToString();

intLeftOver = intLeftOver % 5;

lblPennies.Text = (intLeftOver / 1).ToString();

}




Answer this question

Simple If Statement Question

  • Tim Meyer

    U can simply do it by using validators ..

    y go for this coding ..



  • Anand Raman - MSFT

    Oh, sorry, I mostly code in VB.NET, which doesn't use out. That out variable means the method should look at it as getting a reference to a variable, not getting the value of the variable...essentially, the method is allowed to set the variable you pass it so you can use it outside the method.

    I separated out the code by using the horizontal line button in the posting tools up above. It's the button that looks like a dash.

  • r3n

    Thanks, BlueMikey.  I knew the TryParse method existed for this reason, however I couldn't understand the syntax in my reference material.  To me, the end input looks very different than the definition of the method.

    [Boolean] = Int32.TryParse([string], out [integer]);

    I couldn't make your code work without the "out" shown above, but everything else about your example was perfect.

    I'm very new to coding, and an example of the If statement I was wanting sure clears things up.  However, the example from my reference material looks like the following, and I couldn't accomplish what I wanted with it.


    bool blnVariable;

    blnVariable = Int32.TryParse(strStringVar, out intIntegerVar)

    if (blnVariable == false)

    //code here


    Understanding reference material is key for me teaching myself C#.  I hope it becomes more obvious with experience.  Thanks for your timely help!

    How did you block out that code example   I looked in the FAQs, but didn't see anything on it.  In my first post, I selected "This post contains a code sample".

    Note to others who are as new as I am:  Please be sure not to put a semicolon (;) before your If statement runs the desired code.  Also, be sure to enclose the TryParse method in parenthesis.



  • GraemeH

    What you have done seems to be right. I am not sure what you mean by "what if I wanted the If statement to go though all of the lines of code "

    Please elaborate.

    Thanks,
    Rashmi


  • DTHMTLGOD

    Yeah, there actually is a method that will handle that issue for you.

  • Simple If Statement Question