-2

私はWindowsフォームに取り組んでいます。私は非常に奇妙な問題に直面しています。

ボタンイベントハンドラーの1つで、ifおよびelse条件を適用しました。

問題は、条件と条件の両方が実行されるかどうかです。

誰かが私が間違っている場所を指摘できますか?

private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true && checkEbayName(textBox1.Text) == true )
            {
                DataSet ds = GetUserByEbayName(textBox1.Text);
                if (ds == null)
                    {
                        return;
                    }
                dataGridView1.DataSource = ds.Tables["Customer"];

            }

            if (radioButton2.Checked == true && checkName(textBox1.Text) == true)
            {
                DataSet ds = GetUserByName(textBox1.Text);
                //if (checkCustomer(textBox1.Text, textBox2.Text) == true)
                //{
                if (ds == null)
                {
                    return;
                }
                dataGridView1.DataSource = ds.Tables["Customer"];
            }

            else
            {
                MessageBox.Show("No Customer with matching details");
            }

        }
4

4 に答える 4

6

最初のものが実行されない場合、else が起動ifされます。else if私はあなたがあなたの2番目に使いたいと思うif.

あなたのコードが立っているので、最初のifものは真と評価されるかもしれません。ロジックは 2 番目の に分類されifます。その条件が満たされない場合、else実行されます。

于 2013-02-15T15:03:39.243 に答える
0

一致する if の条件が満たされた場合、else は決して実行されません。ただし、if を分離する必要があり、2 番目は最初のものに依存していません。コードの簡略化されたバージョンは次のようになります

if(a){
   if(d) {
      return;
   }
}

if(b){}
else{
}

a または d のいずれかが false の場合、実行は 2 番目の if に到達します。b も false の場合、else が実行されます。

a と b の両方が false である場合にのみ、else を実行することを意図している場合は、次のようにする必要があります。

if (radioButton1.Checked == true && checkEbayName(textBox1.Text) == true ) {
 //...

} else if (radioButton2.Checked == true && checkName(textBox1.Text) == true) {
  //...
} else {
            MessageBox.Show("No Customer with matching details");
}
于 2013-02-15T15:08:06.883 に答える
0

最初の if 内に return ステートメントが抜けているのではないでしょうか?

if (radioButton1.Checked == true && checkEbayName(textBox1.Text) == true )
{
    DataSet ds = GetUserByEbayName(textBox1.Text);
   if (ds == null)
   {
        return;
   }

   dataGridView1.DataSource = ds.Tables["Customer"];
   return;
}
于 2013-02-15T15:05:09.957 に答える
0

Ifradiobutton1がチェックされている場合は、最初の条件を実行し、ds is not nullコードが返されない場合は 2 番目の if をチェックします。2 つのラジオボタンが相互に排他的である場合、これは真ではなく、else が実行されます。

グリッドに顧客がいない場合にメッセージ ボックスを表示する場合は、最後に null ds をチェックするようにコードを書き直すことができます。

DataSet ds = null;
if (radioButton1.Checked == true && checkEbayName(textBox1.Text) == true )
{
    ds = GetUserByEbayName(textBox1.Text);
    if (ds != null)
        dataGridView1.DataSource = ds.Tables["Customer"];
}
else if (radioButton2.Checked == true && checkName(textBox1.Text) == true)
{
    ds = GetUserByName(textBox1.Text);
    if (ds != null)
        dataGridView1.DataSource = ds.Tables["Customer"];
}

// Show the user that we have not found customers not by ebayname or username
if(ds == null)
    MessageBox.Show("No Customer with matching details");
于 2013-02-15T15:06:33.547 に答える