繁体   English   中英

如何使用 C# 打破 if-else-if

[英]How to break if-else-if using C#

我如何打破 if-else-if ..... 为什么它不起作用? 它只是检查所有条件而不是执行任务。 以下是我的代码。 我已经通过断点检查它移动到所有条件为什么它在满足正确条件后没有停止。 即使它没有进入 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;
        }


    }

我强烈怀疑问题在于Text属性对于任何txt_sel*都永远不会为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将返回一个空字符串 ( "" ) not null

解决方案:

检查""不为null

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

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

编辑:您不必为布尔变量执行==true If 语句默认检查它是否为 true。 使用! 检查false

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;                
    }
    ....
    ....

我认为对于字符串更好地使用 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);

然后定义这些布尔值的条件。 这种情况可以处理意外的空值或空白值。

那是因为它可能找不到任何这些条件为真。

如果它会找到一个真正的条件,它会执行 if 块并且不会事件检查其他。

补充一下其他人所说的,你真的应该在那里有一个最终的 else 语句来捕捉这样的问题:

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

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

 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();
        }


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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