C# 如果使用C,如何中断if-else

C# 如果使用C,如何中断if-else,c#,if-statement,C#,If Statement,如果…为什么它不起作用?它只是检查所有的条件,而不是执行任务。下面是我的代码。我已经通过断点检查了它是否移动到所有条件,为什么它在满足正确条件后没有停止。即使它不进入if活动,它也只是读取所有条件,最后什么也不做 private void ShowHash() { inpic = pb_selected.Image; Bitmap image = new Bitmap(inpic); byte[] imgBytes = new byte[0

如果…为什么它不起作用?它只是检查所有的条件,而不是执行任务。下面是我的代码。我已经通过断点检查了它是否移动到所有条件,为什么它在满足正确条件后没有停止。即使它不进入if活动,它也只是读取所有条件,最后什么也不做

private void ShowHash()
    {
        inpic = pb_selected.Image;
        Bitmap image = new Bitmap(inpic);
        byte[] imgBytes = new byte[0];
        imgBytes = (byte[])converter.ConvertTo(image, imgBytes.GetType());
        string hash = ComputeHashCode(imgBytes);
        txt_selectedText.Text = hash;
        GetHash();
    }

private void GetHash()
    {
        if (txt_sel1.Text == null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null ))
        {
            txt_sel1.Text = txt_selectedText.Text;
            return;
        }

        else if (txt_sel1.Text != null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel2.Text = txt_selectedText.Text;
            return;
        }

        else if (txt_sel2.Text != null && (txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel3.Text = txt_selectedText.Text;
            return;
        }

        else if (txt_sel3.Text != null && (txt_sel4.Text == null || txt_sel5.Text == null))
        {
            txt_sel4.Text = txt_selectedText.Text;
            return;
        }

        else if (txt_sel4.Text != null && (txt_sel5.Text == null))
        {
            txt_sel5.Text = txt_selectedText.Text;
            return;
        }


    }

这是因为它可能没有发现这些条件中的任何一个是正确的


如果它将找到一个真实的条件,它将执行If block,而不会对其他条件进行事件检查。

我强烈怀疑问题在于,对于任何txt_sel*,Text属性都不会为null。假设这些是UI中的文本框,则更有可能的情况是,如果文本框中没有文本,则text属性将返回而不是null。这是大多数UI框架处理空控件的方式

我还建议首先将条件提取到局部变量中:

bool hasSel1 = txt_sel1.Text != "";
bool hasSel2 = txt_sel2.Text != "";
bool hasSel3 = txt_sel3.Text != "";
bool hasSel4 = txt_sel4.Text != "";
bool hasSel5 = txt_sel5.Text != "";

if (!hasSel1 && (!hasSel2 || !hasSel3 || !hasSel4 || !hasSel5)
{
    ...
}
理想情况下,为控件指定更有意义的名称—一组具有相同前缀但带有数字后缀的变量,就可读性而言,很少是一个好主意。

原因:

如果这些文本框中没有任何内容,textbox.Text将返回一个不为null的空字符串

解决方案:

检查是否不为空:

编辑:对于布尔变量,不必执行==true。默认情况下,If语句将其与true进行检查。使用要检查是否存在错误:


为了补充其他人所说的内容,您应该在其中有一个最终的else语句,以抓住以下问题:

else 
{
  throw new InvalidOperationException("My If Statement is Broken");
}

我认为对于字符串,最好使用inbuild null和空格检查函数:

bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2=  string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3=  string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4=  string.IsNullOrWhiteSpace((txt_sel4.Text);
bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);

然后定义这些bool上的if条件。这种情况下可以处理意外的空值或空白值。

感谢大家!我的问题是通过改变条件和你建议的改变来解决的,下面的代码可能会对像我这样的初学者有所帮助

 private void GetHash()
    {
        bool hasValue1 =  string.IsNullOrWhiteSpace(txt_sel1.Text);
        bool hasValue2 =  string.IsNullOrWhiteSpace(txt_sel2.Text);
        bool hasValue3 =  string.IsNullOrWhiteSpace(txt_sel3.Text);
        bool hasValue4 =  string.IsNullOrWhiteSpace(txt_sel4.Text);
        bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);

        if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
        {
            txt_sel1.Text = txt_selectedText.Text;
            return;
        }

        else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5 ))
        {                
                txt_sel2.Text = txt_selectedText.Text;
                return;
        }

        else if (hasValue3 && (!hasValue1 || !hasValue2 || hasValue4 || hasValue5 ))
        {               
                txt_sel3.Text = txt_selectedText.Text;
                return;                
        }

        else if (hasValue4 && (!hasValue1 || !hasValue2 || !hasValue3 || hasValue5 ))
        {
                txt_sel4.Text = txt_selectedText.Text;
                return;
        }

        else if (hasValue5 && (!hasValue1 || !hasValue2 || !hasValue3 || !hasValue4 ))
        {
            txt_sel5.Text = txt_selectedText.Text;
            return;
        }
        else
        {
            CompareHash();
        }


    }

这样做是因为没有一个条件计算为真。按F11键并逐步检查代码,你认为代码做的是错误的,代码做的是错误的,你必须理解代码做的是错误的。它成功了!但在执行第二个条件后,它不会执行第三个条件。当它检查txt_sel1.Text!=它将覆盖txt_sel2.Text的值,而不检查其他值conditions@Sumi:确保未在条件中使用txt_sel2.Text=而不是==。此外,如果没有任何其他代码,可以删除return。由于您使用的是else if,因此如果任何条件都满足,它将不适用于以后的条件。最好使用string.IsNullOrWhiteSpace.used you suggestion,但覆盖txt_sel2.Text的值仍然会出现相同的问题。and=对于布尔值是不可接受的。@Sumi:我在这里找不到任何问题,请尝试使用断点或监视来标识值的更改位置。我用您建议的代码更新了我的问题,但它正在覆盖txt_sel2的值。即使是在下一个条件下,它也有价值。我当时正拼命想弄明白为什么它在你的情况下不起作用。原因是什么?我认为最初的原因是为Textbox.Text签入空值,因为如果显式设置为null,它将为null。@BRING Bull原因是我在第一条中传递了所有错误的条件。@Dreamweaver感谢您的合作。@Dreamweaver问题是,如果else不能正常工作,那么您可以说,为了使if else if正常工作,有两个问题需要解决。
bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2=  string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3=  string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4=  string.IsNullOrWhiteSpace((txt_sel4.Text);
bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);
 private void GetHash()
    {
        bool hasValue1 =  string.IsNullOrWhiteSpace(txt_sel1.Text);
        bool hasValue2 =  string.IsNullOrWhiteSpace(txt_sel2.Text);
        bool hasValue3 =  string.IsNullOrWhiteSpace(txt_sel3.Text);
        bool hasValue4 =  string.IsNullOrWhiteSpace(txt_sel4.Text);
        bool hasValue5 =  string.IsNullOrWhiteSpace(txt_sel5.Text);

        if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
        {
            txt_sel1.Text = txt_selectedText.Text;
            return;
        }

        else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5 ))
        {                
                txt_sel2.Text = txt_selectedText.Text;
                return;
        }

        else if (hasValue3 && (!hasValue1 || !hasValue2 || hasValue4 || hasValue5 ))
        {               
                txt_sel3.Text = txt_selectedText.Text;
                return;                
        }

        else if (hasValue4 && (!hasValue1 || !hasValue2 || !hasValue3 || hasValue5 ))
        {
                txt_sel4.Text = txt_selectedText.Text;
                return;
        }

        else if (hasValue5 && (!hasValue1 || !hasValue2 || !hasValue3 || !hasValue4 ))
        {
            txt_sel5.Text = txt_selectedText.Text;
            return;
        }
        else
        {
            CompareHash();
        }


    }