Jump to content
  • Advertisement
Sign in to follow this  
Instigator

C# .. Try block Problem

This topic is 3682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?)

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!