views:

82

answers:

4

How would I check for errors. If a person enters an incorrect name or doesnt spell it correctly I would like for a messagebox.show to display a message stating "Incorrect name or spelling"

private void button1_Click(object sender, EventArgs e)
    {
        String Andrea;
        String Brittany;
        String Eric;
        if (textBox1.Text == ("Andrea"))
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
        if (textBox1.Text == ("Brittany"))
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
        if (textBox1.Text ==("Eric"))
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();

        {

        } 

    }
+2  A: 

You will need to keep a list or 'dictionary' of correct names.

Then, you can match the text against the entries in the dictionary.

Code would look similar to the following:

HashSet<string> correctNames = ;// initialize the set with the names you want

private void button1_Click(object sender, EventArgs e)
{
    if (correctNames.Contains(textBox1.Text))
        Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    else 
    {
       MessageBox.Show("The speling of the naem " + textBox1.Text + " was incorect", "Bad Spelling Error");
    }
}

You probably want to use correct spelling in your implementation.

Have a look at the documentation for HashSet to get a better idea of how to use it.

jjnguy
+1 It would be funny if the message was "Bad Speling Eror"
SwDevMan81
@SwDev, fixed `:P`
jjnguy
Haha nice! Good recommendation on HashSet as well, the lookup is probably faster than list.Contains()
SwDevMan81
@SwDev, yeah, the look up will be a bit faster. But the time difference will be minimal on any small set.
jjnguy
A: 

Throw all the names in a collection, like a List or Dictionary, then use the .Contains() method. That should provide a neater solution.

npinti
+1  A: 

This will check if any name in the list equals the textBox entered name:

List<string> nameList = new List<string>();

nameList.Add("Andrea");
nameList.Add("Brittany");
nameList.Add("Eric");

if (nameList.Contains(textBox1.Text))
{
    //Process name here.
}
else
{
    //Show messagebox here.
}
Ocelot20
... or use `Contains`.
Timwi
Yeah realized that right after posting, hah.
Ocelot20
A: 

Thanks everyone

randywhite30
It's nice of you to thank them, but this is not an answer to your question. This should have been posted as comments to the real answers. You should accept the most helpful answer and mark it as the "best answer", and vote up all good answers that helped you in some way. This gives those kind people points for their efforts and encourages them to keep on helping.
Rich