0

I have a simple Windows Forms application with a form that has two TextBoxes to find postcode/suburb name:

  1. Enter the suburb name
  2. Enter the postcode

If the postcode (TextBox) has no value entered the form is hung up and it won't submit/return values. Tried a few things but still fails (FormatException was unhandled). Adding any int value it works fine.

private void btnFind_Click(object sender, EventArgs e)
{ 
    //DECLARE ARRAY
    string[] arrSuburbName = new string[5];
    int[] arrSuburbPC = new int[5];

    //POPULATESUBURB NAME ARRAY
    arrSuburbName[0] = "DEE WHY";
    arrSuburbName[1] = "SYDNEY";
    arrSuburbName[2] = "HURSTVILLE";
    arrSuburbName[3] = "BALMAIN";
    arrSuburbName[4] = "NORTH SYDNEY";

    //POPULATE POSTCODE ID ARRAY
    arrSuburbPC[0] = 2099;
    arrSuburbPC[1] = 2000;
    arrSuburbPC[2] = 2220;
    arrSuburbPC[3] = 2041;
    arrSuburbPC[4] = 2060;

    //VARIABLES
    string inputSuburb = "";
    int inputPostCode = 0;
    string msg = "";

    //INPUT
    inputSuburb = txtInputSuburb.Text.Trim();
    inputPostCode = int.Parse(txtInputPostCode.Text);


    //PROCESS
    for (int i = 0; i < arrSuburbPC.Length; i++)
    {
        if (inputSuburb.ToUpper() == arrSuburbName[i])
        {
            msg = "Postcode for " + arrSuburbName[i] + " is: " + arrSuburbPC[i];
            inputPostCode = 0;
            break; //EXIT THE LOOP
        }
        else if (inputPostCode == arrSuburbPC[i])
        {
            msg = "Postcode for " + arrSuburbName[i] + " is: " + arrSuburbPC[i];
            break; //EXIT THE LOOP
        }
        else
        {
            msg = "Postcode Not Found";
        }
    }

    //OUTPUT
    lblResult.Text = msg;
}

enter image description here

ivayle
  • 1,070
  • 10
  • 17
Kerry7777
  • 4,246
  • 1
  • 18
  • 28
  • 1
    Because there is no value within your `txtInputPostCode.Text` and you are trying to parse it, make a check before parsing whether the `textBox` has any value or not and then parse if it has some value. – M. Adeel Khalid Feb 28 '17 at 06:16
  • what would you like to be the default value `inputPostCode` if it is empty? in such cases you need to use any conditional statement like @J.SMTBCJ15 answered and yes you cant parse a null object. – Shift 'n Tab Feb 28 '17 at 06:16

2 Answers2

0

Why don't you use Int.TryParse(...) instead of Int.Parse(...)?

Parse v. TryParse

Community
  • 1
  • 1
J.SMTBCJ15
  • 471
  • 6
  • 20
0

You have

inputSuburb = txtInputSuburb.Text.Trim();
inputPostCode = int.Parse(txtInputPostCode.Text);

but in the UI screen you attached you gave OR so it can happen that postcode will be empty.

You need to change you logic in code. Things to consider:

  1. What to do if post code is empty? -> this is your situation right now
  2. What if user enter non-number like 'abcd'

Your logic should have some validation. Use try catch construction for example.

Maybe you should also consider user NumericUpDown control?

klm_
  • 1,199
  • 1
  • 10
  • 26
  • I am going to have to think about this some more after the comments / suggestions. Any links on simple snippets to add some validation would be appreciated? Be cool if C# had something like "required". 1. Work out the logic 2. Test if either textbox has value (use one or the other) 3. If Postcode is empty parse value - validate += value 4. If Postcode has alphabetical character return messagebox/alert - validate 5. If Suburb has numerical character return messagebox/alert - validate – Kerry7777 Feb 28 '17 at 08:33
  • Q. try catch - something like this? catch (InvalidCastException e) { if (e.Data == null) { throw; } else { // Take some action. } } – Kerry7777 Feb 28 '17 at 08:34
  • Yes, for example. The point is that you miss logic on how system should work when Postcode is present or absent. – klm_ Feb 28 '17 at 09:18