Skip to main content
gamedev.net
🔒 Locked

C# .. Try block Problem

Started by Instigator Jul 17, 2008 at 9:13 AM 2 replies 1.4k views
Original Post
Instigator
Instigator
I've got an application which has a Name, Wage and hourly pay field (txtName, txtHourlyWage, txtHours) .. and I'm trying to determine if the user enters incorrect data into these field.. e.g, he enters alphabetical characters as a double value. So, I decided to use a catch block in a loop to store all errors. I have an array called blnFlags set to false before hand to set all flags to 'currently no error'

 private void cmdCalc_Click(object sender, EventArgs e)
        {
            int errorFlag = 0; 
            String tmpName;
            double tmpWage;
            double tmpHourls;
            Boolean [] blnFlags = new Boolean[3]; // will hold the type of error
            
            for (int i = 0; i < 3; i++) // initialize the flags array to false (no error)
                blnFlags = false;

            for (int x = 0; x < 3; x++)
            {
                try
                {
                    if (x == 0)
                    {
                        tmpName = txtName.Text;
                    }
                    else if (x == 1)
                    {
                        tmpWage = Double.Parse(txtHourlyWage.Text);
                    }
                    else if (x == 2)
                    {
                        tmpHours = Double.Parse(txtHoursWorked.Text);
                    }                 
                }
                catch (FormatException e)
                {
                    blnFlags[x] = true; // assigns the appropriate flag based on the loops variable
                }
            }
            MessageBox.Show("Title", "Error Code: " + blnFlags[0] + ", " + blnFlags[1] + ", " + blnFlags[2] + ". ", MessageBoxButtons.OKCancel);
        }
However, can I do this? For some reason it says that FormatException can't be placed within this scope again. How do I get rid of this error
Quote:
Form1.cs(35,29): error CS0136: A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else Form1.cs(53,24): error CS0246: The type or namespace name 'e' could not be found (are you missing a using directive or an assembly reference?)
davek20
davek20
The problem is that in the "catch(FormatException e)", you are declaring another variable named "e" which is already a variable in the method parameters "EventArgs e".

Just change the catch line to this "catch(FormatException fe)" or something else.
samnova
samnova
I don't know any C# but based on the errors then how about trying to change:

catch (FormatException e)

to something like:

catch (FormatException fex)

The error complains that the FormatException gives a new meaning to e which is your EventArgs
www.SamuelNova.com
Instigator
Instigator
THANKS.. wow, what a stupid question. I honestly looked at this for 2 hours.. I must have not gotten enough sleep last night.

[Edited by - Instigator on July 17, 2008 9:27:05 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.