未分配的局部变量错误 [英] Unassigned local variable error

查看:94
本文介绍了未分配的局部变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:



 foreach(dataTable.Rows中的Software.dsBDD.list_table21行)
{
PictureBox盒;
MemoryStream流;
面板小组;
标签标签;
bool?通过;
if(this.pass.HasValue&& this.end)
{
pass = this.pass;
}
if((pass.GetValueOrDefault()&& pass.HasValue)|| row.view_only)





GetValueOrDefault附近的变量pass下划线为错误:使用未分配的局部变量传递。我不明白为什么这个变量是未分配的,因为在同一行中HasValue附近有pass并且它已被分配。我的语法错误在哪里?!

解决方案

您应该将第二个IF块移到第一个IF块中,因为本地传递变量仅在第一个IF内初始化。



所以你应该像这样修改你的代码(我在你提供的解决方案中更新代码后更新了代码):

  bool ? pass =  null ; 
foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
{
PictureBox框;
MemoryStream流;
面板小组;
标签标签;
如果 .pass.HasValue&& .end)
{
pass = .pass;
}
// 在使用之前必须测试null!
if (pass!= null && pass.GetValueOrDefault()|| row.view_only )
// ..


BOOL? pass = null; 应解决此问题。


解决方法如下:



i`ve defined在foreach范围之前通过。



  bool ? pass =  null ;  //   ---已添加 
foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
{
PictureBox box;
MemoryStream流;
面板小组;
标签标签;
// bool?通过; ---已删除
如果 .pass.HasValue& ;& .end)
{
pass = .pass;
}
if ((pass.GetValueOrDefault()&& pass.HasValue)|| row.view_only)
}


i have this:

foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
            {
                PictureBox box;
                MemoryStream stream;
                Panel panel;
                Label label;
                bool? pass;
                if (this.pass.HasValue && this.end)
                {
                   pass = this.pass;
                }
                if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)



Variable "pass" near "GetValueOrDefault" underlined as an error: "Use of unassigned local variable pass". I don`t understand why this variable is unassigned because in the same line there is "pass" near "HasValue" and it is assigned. Where is my syntax error?!

解决方案

You should move the 2nd IF block inside the 1st one, because the local pass variable is initialized only inside the 1st IF.

So you should modify your code like this (I updated the code after you update your code in your provided solution):

bool? pass = null;
foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
            {
                PictureBox box;
                MemoryStream stream;
                Panel panel;
                Label label;
                if (this.pass.HasValue && this.end)
                {
                   pass = this.pass;
                }
                // Must test for null before to use it!
                if (pass != null && pass.GetValueOrDefault() || row.view_only)
                //..


bool? pass = null; should fix this issue.


Solved like this:

i`ve defined pass before foreach scope.

bool? pass = null; // ---THIS WAS ADDED
foreach (Software.dsBDD.list_table21 row in dataTable.Rows)
        {
            PictureBox box;
            MemoryStream stream;
            Panel panel;
            Label label;
            // bool? pass;   ---THIS WAS REMOVED
            if (this.pass.HasValue && this.end)
            {
               pass = this.pass;
            }
            if ((pass.GetValueOrDefault() && pass.HasValue) || row.view_only)
        }


这篇关于未分配的局部变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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