RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Unassigned Local Variable ??

Sorry but I have a brain f**t happening.

Can anyone see why this snip of code . .
Code:
public static int GetArtifactChance( Mobile boss )
{
if ( !Core.AOS )
return 0;
 
int luck = LootPack.GetLuckChanceForKiller( boss );
 
int chance;
 
if (boss is DemonKnight)
{
switch ( Utility.Random( 5 ) )
{
case 0: chance = 1000 + (luck); break;
case 1: chance = 1000 + (luck - 10); break;
case 2: chance = 1000 + (luck - 30); break;
case 3: chance = 1000 + (luck - 50); break;
case 4: chance = 1000 + (luck - 70); break;
}
}
else
{
chance = 5500 + (luck / 10);
}
//Line 151 follows:
return chance;
}

. . is giving me this error?
Code:
Errors:
 + Customs/Various NPC/Elven/Elven Drow/DF Gate Keeper/DemonKnight.cs:
    CS0165: Line 151: Use of unassigned local variable 'chance'

Thank you
 

_Epila_

Sorceror
Let me try to explain
You have declared in your script the 'chance' variable

int chance;

But it contains no value (unassigned)

When you 'switch ( Utility.Random( 5 ) )' the compiler does not know that this will return a value between 0 and 4 as your cases says, even that you can see it, the compiler can't, it thinks that any value can be returned from the random (could be any other) function. I know it sounds dumb but...

So imagine that you're the compiler and you have a variable with no value. You cover just cases from 0 to 4 but what for the other 4 billions cases (int range is from -2 billion to 2 billion // int.MinValue to int.MaxValue)? No value will be assignable to the variable. How can you return a variable with no value?

You have two options:

Declare and initialize the variable with a default value
int chance = 0; //You will ensure that 'chance' is never empty (remember that empty is not zero)

or

create a default case on your switch

Code:
switch ( Utility.Random( 5 ) )
{
case 0: chance = 1000 + (luck); break;
case 1: chance = 1000 + (luck - 10); break;
case 2: chance = 1000 + (luck - 30); break;
case 3: chance = 1000 + (luck - 50); break;
case 4: chance = 1000 + (luck - 70); break;
default: chance = 0; break; //Here will be every other possible value that an 'int' can have, even if you ensure that it will never happen
}

Again, it sounds dumb and obvious but the compiler does not know that and it will cover any possible situation

Code:
int myFunc(x)
{
  int y;
  if (x == 1)
  {
      y = 2;
  }
  return y; //what if x is not '1' ? what value will be returned?
}


MSDN Compiler Error CS0165 said:
Note that this error is generated when the compiler encounters a construct that might result in the use of an unassigned variable, even if your particular code does not.
Hope it make things clearer :)


PS:
Google for MSDN CS0165, it will give you a more detailed description about this error than mine
 
Two things . .

1) Wow thanks that solved my problem *smiles*
2) I don't think I would have ever made the logic on that by myself.

I really appreciate it and the quick reply

Cheers
 

daat99

Moderator
Staff member
What _Epila_ said is correct.
It's always a good practice to use "default" in a switch case even if that "default" does nothing.

Another solution (which might be better in this particular case) is to use this code:
Code:
public static int GetArtifactChance( Mobile boss )
{
if ( !Core.AOS )
return 0;
 
int luck = LootPack.GetLuckChanceForKiller( boss );
 
int chance = 5500 + (luck / 10);
 
if (boss is DemonKnight)
{
     switch ( Utility.Random( 5 ) )
     {
          case 0: chance = 1000 + (luck); break;
          case 1: chance = 1000 + (luck - 10); break;
          case 2: chance = 1000 + (luck - 30); break;
          case 3: chance = 1000 + (luck - 50); break;
          case 4: chance = 1000 + (luck - 70); break;
          default: break;
     }
}
return chance;
}
You should notice that I always assign the default value of "chance = 5500 + (luck / 10);".
Then if the boss is a DemonKnight I go over the switch case as you posted it with the added default case that does nothing.
This is to illustrate that there is a default case so you can write whatever you want in it to use something else.
This will help you remember that when you change the random number from 5 to something else you will need to add/remove the relevant switch cases.

The code I posted is less efficient when we consider performance because I always perform the assignment of "5500 + (luck/10)" which involves dividing and adding operations (the dividing operation isn't the cheapest CPU instruction out there).

The benefit of this code is the readability.

It is up to you to decide if you prefer readability or a tiny bit of performance boost (which you might not notice ever).
 
Top