2

Helloo, everyone. I have a problem with If statement. Where I'm wrong?

 int cnt = 0;
 int wayListNums;

 foreach (TableRow tr in allVisibleRows)
 {

   SPListItem itemAdd = wayList.Items.Add();

   if (cnt == 0) 
   {
      wayListNums = itemAdd.ID; 
   }      

   itemAdd["wayNum"] = "WayBill № " + " " + wayListNums;

   ...................

This is following error :

Error 1 Use of unassigned local variable 'wayListNums' ....

Gohyu
  • 468
  • 2
  • 10
  • 32
  • 1
    replace int wayListNums; with int wayListNums = 0; – Uthistran Selvaraj Apr 27 '16 at 05:56
  • 1
    You need to assign a value to wayListNums before you use it. Think about what would happen if cnt does not equal 0 on the first loop iteration. – TimS Apr 27 '16 at 05:56
  • Possible [duplicate](http://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable). Find out the root cause of bug clearly and try to fix it. The issue is not with if statement as you mentioned in the title. – Arun Prasad Apr 27 '16 at 07:00

5 Answers5

2

The problem here is that wayListNums has no default value when it's declared. Later you are setting it's value only in if block, so if that if is not executed, the variable remains unassigned, like the error tells you.

So you have 2 options:

  1. Assign some value for wayListNums on declaration.

int wayListNums = 0;

  1. Assure, that every code branch will assign some value to the variable, before accessing it:

//code

if (cnt == 0) 
   {
      wayListNums = itemAdd.ID; 
   } 
 else
   {
      wayListNums = somethingElse;
   }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
3615
  • 3,787
  • 3
  • 20
  • 35
  • For the second point: what is the use of checking condition there? true and false states assigns the same value to the variable – sujith karivelil Apr 27 '16 at 06:03
  • I've edited the answer, else should obviously return some other value. The idea is that you are not restricted to assign the value uppon declaration, as you suggested, you may also leave it unassigned, but to make sure every code path have it assigned before usage. – 3615 Apr 27 '16 at 06:04
0

The compiler not sure about the values in the collection allVisibleRows sometimes it may be null or empty, (the variable get value only when the collection is non-empty as well as if (cnt == 0) evaluates to true.) In such situations the value of wayListNums is unknown. this is the reason for the warning. to overcome this issue, Initialize the variable before use:

int wayListNums=0;// or some other value

So that the default value will be used if other conditions failed.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You have to assigned default value to local variable..as error suggest it is unassigned.. here int wayListNums = 0;// because of int default value 0

Dhara
  • 1,914
  • 2
  • 18
  • 37
0

In C# and other similar languages the compiler expects the value of every variable to be initialized before it can be used. In your case the compiler is not sure if the value initilized within if-block will always be initialized and since you are using it outside the scope of this if-block the compiler simply gives you an error that value is not initialized. Simply setting

    int wayListNums  = 0;

will resolved the issue.

usamazf
  • 3,195
  • 4
  • 22
  • 40
0

assigns a value to int

int wayListNums = 0;
wildhawk
  • 79
  • 1
  • 5