错误显示为使用未分配的局部变量'roc' [英] Error shows as Use of unassigned local variable 'roc'

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

问题描述

DateTime roc;  
       DateTime arpa; 

        for (int i = 0; i < objDT.Rows.Count; i++)
        {
            if (objDT.Rows[i][0].ToString().Contains("+") == true)
                Session["course"] = "Package";
            else
                Session["course"] = "Course";

            if (objDT.Rows[i][0].ToString().Trim() == "ARPA")
            {
                arpa = Convert.ToDateTime(objDT.Rows[i][4].ToString().Trim()); //added
                CheckArpa = true;
            }

            if (objDT.Rows[i][0].ToString().Trim() == "ROC")
            {
                roc = Convert.ToDateTime(objDT.Rows[i][5].ToString().Trim()); //added
                CheckRoc = true;
            }

                 
               if (roc < arpa) 
               {
                   ScriptManager.RegisterStartupScript(this, GetType(), "Invalid date", "alert('First Book Roc than the ARPA Course')", true);
                   return;
                }           

        }







当我运行错误时显示如下;



使用未分配的局部变量''roc''

使用未分配的局部变量''arpa''



从我的代码我在第一个DateTime roc中声明; DateTime arpa;



为什么上面这个错误显示我上面的代码有什么问题。



请帮助我。



问候,

Narasiman P




When i run the error shows as follows;

Use of unassigned local variable ''roc''
Use of unassigned local variable ''arpa''

From my code i am declaring in the first DateTime roc; DateTime arpa;

why this above error shows what is the problem in my above code.

Please help me.

Regards,
Narasiman P

推荐答案

在你的代码可以到达 if(roc< arpa)行,而无需为这些变量中的任何一个或两个设置值。您需要在声明中初始化它们,例如:

In your code it is possible to reach the line "if (roc < arpa) " without setting a value for either or both of these variables. You need to initialise them in the declarations like:
DateTime roc = DateTime.Now;
DateTime arpa = roc;


你没有初始化变量,所以它们有可能在它们被初始化之前被使用导致意外行为,例如崩溃。



如果 roc arpa ,以下行无效没有有效价值。



You''re not initialising the variables, so there is a possibility that they will be used before they are initialised, causing unexpected behaviour, such as a crash.

The following line would be invalid if roc or arpa do not have a valid value.

if (roc < arpa) 

您可以在for循环中执行两个变量的比较,并且在每次迭代中只能设置其中一个值。之后比较它们。
You execute the comparison of the two variables in the for loop, and in every iteration only one of the values can be set. Compare them afterwards.


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

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