简体   繁体   中英

This member is defined more than once error with variable in if statement

The first button 2 and 1 get this error in the first and second if statements. How can I get them to work? Thanks in advance.

    private void button1_Click(object sender, EventArgs e)
    {
        if (button2 == true)
        {
            richTextBox1.AppendText(String.Format("Please continue with the midpoint.\n"));

        }
        else if(button2 == false)
        {
            richTextBox1.AppendText(String.Format("Type the first set of coordinates in.\n"));
            bool button1 = true;
        }


    }

    private void button2_Click(object sender, EventArgs e)
    {
          if (button1 == true)
        {
            richTextBox1.AppendText(String.Format("Please continue with the distance.\n"));

        }
        else if(button1== false)
        {
            richTextBox1.AppendText(String.Format("Type the first set of coordinates in.\n"));
            bool button2 = true;
        }

    }
}

}

bool button1 = true;

我假设您已经在查看样本的某处定义了bool值,在这种情况下,您无需再次指定类型:

button1 = true

You need to define these members at class level as private member may be as well as give them a different name/identifier since button1 and button2 most probably be your button control name.

public class someclass
{
private bool mbutton2 = false;
private bool mbutton1 = false;

    private void button1_Click(object sender, EventArgs e)
    {
        if (mbutton2)
        {
            richTextBox1.AppendText(String.Format
                      ("Please continue with the midpoint.\n"));
        }
        else
        {
            richTextBox1.AppendText(String.Format
                  ("Type the first set of coordinates in.\n"));
            mbutton1 = true;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
          if (mbutton1)
        {
            richTextBox1.AppendText(String.Format
                    ("Please continue with the distance.\n"));

        }
        else
        {
            richTextBox1.AppendText(String.Format
                  ("Type the first set of coordinates in.\n"));
            mbutton2 = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM