请帮助我如何使我的代码部分工作,因为我不断收到警告消息“检测到无法访问的代码”。 [英] Please, I need assistance on how I can get this part of my code work, as I keep getting warning message "unreachable code detected"

查看:66
本文介绍了请帮助我如何使我的代码部分工作,因为我不断收到警告消息“检测到无法访问的代码”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private bool ValidData()
        {
                 
            // Initialize function return value
            return false;
            
            // Test CustomerID is complete
            if (((service_NoTextBox.Text == String.Empty) 
                    ||(Information.IsNumeric(service_NoTextBox.Text) == false)))
            {
                // Required employee ID is not complete
                MessageBox.Show("Service Number is not complete", "Promotion List Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                service_NoTextBox.Focus();
                service_NoTextBox.SelectAll();
            }
            else if (((file_NoTextBox.Text == String.Empty)
                        || (Information.IsNumeric(file_NoTextBox.Text) == false)))
            {
                // Validate File Number
                MessageString = "File Number is required and must be in five digit format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                file_NoTextBox.Focus();
                file_NoTextBox.SelectAll();
            }
            else if (((nameTextBox.Text == String.Empty)
                        || (Information.IsNumeric(nameTextBox.Text) == true)))
            {
                // Validate  Name
                MessageString = "Name is required in alphabet format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                nameTextBox.Focus();
                nameTextBox.SelectAll();
            }
            else if (((new_RankTextBox.Text == String.Empty)
                        || (Information.IsNumeric(new_RankTextBox.Text) == true)))
            {
                // Validate New Rank
                MessageString = "New Rank is required in alphabet format.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                new_RankTextBox.Focus();
                new_RankTextBox.SelectAll();
            }
            else if (((old_RankTextBox.Text == String.Empty)
                        || (Information.IsNumeric(old_RankTextBox.Text) == true)))
            {
                // Validate Previous Rank
                MessageString = "Old Rank is required in alphabetic form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                old_RankTextBox.Focus();
                old_RankTextBox.SelectAll();
            }
            else if (((stationTextBox.Text == String.Empty)
                        || (Information.IsNumeric(stationTextBox.Text) == true)))
            {
                // Validate State
                MessageString = "State is required in letter form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                stationTextBox.Focus();
                stationTextBox.SelectAll();
            }
            else if (((comboBox1.Text == String.Empty)
                        || (Information.IsNumeric(comboBox1.Text) == true)))
            {
                // Command Is required
                MessageString = "Please Enter Command.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBox1.Focus();
                comboBox1.SelectAll();
            }
            else if (((comboBox2.Text == String.Empty)
                        || (Information.IsNumeric(comboBox2.Text) == true)))
            {
                // Validate 
                MessageString = "State of Origin must be in Alphabet form.";
                MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBox2.Focus();
                comboBox2.SelectAll();
            }
                
            else
            {
                // All of the data is valid
                return true;
            }
                
        }





我尝试过: < br $>


我试图设置ValidData = false和true,但我也得不能分配给方法类型



What I have tried:

I have tried to set the ValidData = false and true, but i also get cannot assign to a method type

推荐答案

你方法的第一行是 return false; ,所以你的方法会立即将 false 返回给调用者,此行后的所有内容都将被忽略(无法访问)。



只有当你返回 false 时请注意'数据'实际上是无效的。



上面的'return false'上面的注释表示'初始化函数返回值',所以如果这是你想要做的,然后你可以在顶部和<+ c $ c>返回returnValue; (并在必要时在方法中更改 returnValue 。)
The first line of your method is return false;, so your method will immediately return false to the caller, and everything after this line will be ignored ("unreachable").

Only return false when you notice that the 'data' is actually invalid.

The comment above your 'return false' says 'Initialize function return value', so if that's what you want to do, then you can do something like bool returnValue = false; at the top, and at the end of the function, return returnValue; (and change returnValue in your method when necessary).


更新的解决方案根据Dave Kreskowiak的评论,从每个条件块中删除 return 关键字。

将其替换为返回变量并在必要条件下使用它阻止并返回变量。



updated solution as per the comments from Dave Kreskowiak, removed the return keyword from each condition block.
replace it with a return variable and use it in required condition block and return the variable.

private bool ValidData()
       {

           // Initialize function return value
           bool returnValue = false;

           // Test CustomerID is complete
           if (((service_NoTextBox.Text == String.Empty)
           || (Information.IsNumeric(service_NoTextBox.Text) == false)))
           {
               // Required employee ID is not complete
               MessageBox.Show("Service Number is not complete", "Promotion List Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               service_NoTextBox.Focus();
               service_NoTextBox.SelectAll();
           }
           else if (((file_NoTextBox.Text == String.Empty)
           || (Information.IsNumeric(file_NoTextBox.Text) == false)))
           {
               // Validate File Number
               MessageString = "File Number is required and must be in five digit format.";
               MessageBox.Show(MessageString, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               file_NoTextBox.Focus();
               file_NoTextBox.SelectAll();
           }
               .
               .
               .
               .

           else
           {

               returnValue = true;
           }
           return returnValue;

       }
   }


这篇关于请帮助我如何使我的代码部分工作,因为我不断收到警告消息“检测到无法访问的代码”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆