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