Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
                }           

        }



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
Posted
Updated 5-Apr-13 2:29am
v2
Comments
[no name] 5-Apr-13 8:30am    
The error means exactly what is says. You have declared your variable but they are not assigned a value.

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:
C#
DateTime roc = DateTime.Now;
DateTime arpa = roc;
 
Share this answer
 
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) 
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900