none
Unassigned variable error on out type variables RRS feed

  • Question

  • In below code on compilation I am getting error on line "var s = x + y + z;" 
    Error states :'local variables are unassigned' on variables 'y' and 'z', but not on variable 'x'.Can some one explain the reason for this behavior.

    Behavior is same if I am using '&&' instead of '||'.

    static void Main(string[] args)
            {
    
                decimal x, y, z;
    
                bool output = ConvertTodec("1.2", out x) || ConvertTodec("1.2", out y) || ConvertTodec("1.2", out z);
    
                if (output)
                {
                    var s = x + y + z;
                }
    }
     private static bool ConvertTodec(string str,out decimal a)
            {
                return decimal.TryParse(str, out a);
            }




    • Edited by Sukhdeep.it Tuesday, April 10, 2018 6:02 AM
    Tuesday, April 10, 2018 5:10 AM

Answers

  • The compiler doesn't know until run time if all the statements will be executed, so it assumes the worst. If a variable is only given a value on some condition, it assumes the condition might not be met and the variable won't be initialised.

    Here's another example.

             int a, b = 0, c = 1;
             if (c > b)
             {
                a = 2;
             }
             int d = a + b + c; // Error here "Use of unassigned local variable 'a'", 
                                //even though (c > b) will be true.

    • Marked as answer by Sukhdeep.it Wednesday, April 11, 2018 3:14 AM
    Tuesday, April 10, 2018 6:15 AM

All replies

  • It's because if this bit...

    ConvertTodec("1.2", out x)

    ...is true, the rest of the line won't be executed because "A or B or C" must be true if A is true.

    You can get around the problem like so.

             decimal x, y, z;
    
             bool b1 = ConvertTodec("1.2", out x);
             bool b2 = ConvertTodec("1.2", out y);
             bool b3 = ConvertTodec("1.2", out z);
    
             bool output = b1 || b2 || b3;
    
             if (output)
             {
                var s = x + y + z;
             }


    • Edited by Ante Meridian Tuesday, April 10, 2018 5:33 AM too many x's
    Tuesday, April 10, 2018 5:31 AM
  • At compilation time this will not be evaluated.Also behavior is same if I write ConvertTodec("A", out x), that will be false.
    Tuesday, April 10, 2018 5:58 AM
  • The compiler doesn't know until run time if all the statements will be executed, so it assumes the worst. If a variable is only given a value on some condition, it assumes the condition might not be met and the variable won't be initialised.

    Here's another example.

             int a, b = 0, c = 1;
             if (c > b)
             {
                a = 2;
             }
             int d = a + b + c; // Error here "Use of unassigned local variable 'a'", 
                                //even though (c > b) will be true.

    • Marked as answer by Sukhdeep.it Wednesday, April 11, 2018 3:14 AM
    Tuesday, April 10, 2018 6:15 AM
  • Try this too:

    bool output = ConvertTodec("1.2", out x) & ConvertTodec("1.2", out y) & ConvertTodec("1.2", out z);

    If you really wanted '||', then use '|'.




    • Edited by Viorel_MVP Tuesday, April 10, 2018 6:53 AM
    Tuesday, April 10, 2018 6:47 AM