2

Just a newbie to c# looking for some guidance.

In the below code if I used double area; it would give me the error : "Use of unassigned local variable area". I had to put double area = 0;

My question is why? Why do I have to assign it a value. I have never had to do that before so it's a bit confusing to me.

Thanks in advance for your response.

      int userValue, rad, heightOfRectangle, widthOfRectangle, baseOfTriangle, heightOfTriangle;
        double area = 0;

        Console.Write("\n\n");
        Console.Write("Calculating Area of Geometrical Shape\n");
        Console.Write("=======================================\n");
        Console.Write("\n\n");
        Console.Write("Please select 1 for Circle, 2 for Rectangle and 3 for Triangle: ");

        userValue = Convert.ToInt32(Console.ReadLine());

        switch (userValue)
        {
            case 1:
                Console.Write("Please Enter Radius of Circle: ");
                rad = Convert.ToInt32(Console.ReadLine());
                area = 3.14 * rad * rad;
                break;

            case 2:
                Console.Write("Please enter Height of Rectangle: ");
                heightOfRectangle  = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter Width of Rectangle: ");
                widthOfRectangle = Convert.ToInt32(Console.ReadLine());
                area = widthOfRectangle * heightOfRectangle;
                break;

            case 3:
                Console.Write("Please enter Base of Triangle: ");
                baseOfTriangle = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter height of Triangle: ");
                heightOfTriangle = Convert.ToInt32(Console.ReadLine());
                area = .5 * baseOfTriangle * heightOfTriangle;
                break;

        }

        Console.WriteLine("\nThe area is {0}", area);
JAL
  • 101
  • 7

3 Answers3

4

Imagine that user input 123 on

"Please select 1 for Circle, 2 for Rectangle and 3 for Triangle:

question. In this case area will not have been assigned (no case will be executed) and

 Console.WriteLine("\nThe area is {0}", area);

will not have an idea what to output

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

As stated, it is good practice to initialize the variable to ensure it has a value and avoid a possible exception error by having it called without being assigned any value. In addition, adding a "goto" placement will allow you to return to the starting point (although I'd rather you create separate methods for this)

        int userValue, rad, heightOfRectangle, widthOfRectangle, baseOfTriangle, heightOfTriangle;
        double area;

        Console.Write("\n\n");
        Console.Write("Calculating Area of Geometrical Shape\n");
        Console.Write("=======================================\n");
        Console.Write("\n\n");

        Start:

        Console.Write("Please select 1 for Circle, 2 for Rectangle and 3 for Triangle: ");

        userValue = Convert.ToInt32(Console.ReadLine());

        switch (userValue)
        {
            case 1:
                Console.Write("Please Enter Radius of Circle: ");
                rad = Convert.ToInt32(Console.ReadLine());
                area = 3.14 * rad * rad;
                break;

            case 2:
                Console.Write("Please enter Height of Rectangle: ");
                heightOfRectangle = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter Width of Rectangle: ");
                widthOfRectangle = Convert.ToInt32(Console.ReadLine());
                area = widthOfRectangle * heightOfRectangle;
                break;

            case 3:
                Console.Write("Please enter Base of Triangle: ");
                baseOfTriangle = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter height of Triangle: ");
                heightOfTriangle = Convert.ToInt32(Console.ReadLine());
                area = .5 * baseOfTriangle * heightOfTriangle;
                break;
            default:
                goto Start;
        }

        Console.WriteLine("\nThe area is {0}", area);
        Console.Read();
Innat3
  • 3,561
  • 2
  • 11
  • 29
0

The reason for error is that it can happen that non of the cases in your switch statement occur - e.g. the user makes a wrong input.

Also local variables are not initialized with the default value, thus area has no value if non of the case statements are executed Why compile error "Use of unassigned local variable"?

Community
  • 1
  • 1
Bojan B
  • 2,091
  • 4
  • 18
  • 26
  • Variable `area` is a value-type, it will never be `null`, because it's not a valid value. It will simply be uninitialized and not pass the compilation. – kiziu Sep 12 '16 at 09:07