2 Replies - 687 Views - Last Post: 20 January 2011 - 05:00 AM Rate Topic: -----

#1 Guest_Wanna-B-coder*


Reputation:

Variable error with If Else

Posted 19 January 2011 - 08:13 PM

Okay, I am having some serious problems here. I am new at this and just playing around because I am trying to learn some code.

I am trying to learn how to work with If Else statements.

The problem that I am having is this. I define the variable in the beginning, then I used it in an IF statement. When I try to use it later in the code again, I get "Use of unassigned variable..."

here it is
        const double SS_CATA = 3, SS_CATB = 1.45, SS_SUR = 2.5;
        string method, category, surcharge;
        double charge, planes, cars;

        category = "a";
        method = "1";
        surcharge = "y";
             
        if (category != "b" && category != "B")
        {
            Console.WriteLine("Please enter the number of cars:");
            cars = double.Parse(Console.ReadLine());
        }
        else
        {
            Console.WriteLine("Please nter the number of planes:");
            planes = double.Parse(Console.ReadLine());
        }



        if (method == "1" && category == "a" && surcharge == "y")
        {
            charge = cars * SS_CATA + SS_SUR;
        }
        else
        {
            charge = planes * SS_CATA + SS_SUR;
        }

        Console.WriteLine(charge);



In the second set of If, Else, the variables cars and planes both get that error.

What am I doing wring?

Is This A Good Question/Topic? 0

Replies To: Variable error with If Else

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Variable error with If Else

Posted 19 January 2011 - 10:46 PM

ok, the problem is that you're setting the values inside of the if statement. If for some reason you're if statements didn't execute then the values of those variables would never get set. To prevent that from happening the compiler requires that the variable have a value that stays in scope with where it is being used. The scope of the variable is generally considered to be kept within whichever block sets the value of the variable. Since you don't set the value until the variable is inside the first if/else block the scope of the variable is kept within that block and doesn't pass back to the main part of the method. Just set the variables to 0 when you declare them at the beginning and the scope of the variable will pass to any blocks contained within the method and that should solve your problem.

double charge=0, planes=0, cars=0;

This post has been edited by Nakor: 19 January 2011 - 11:03 PM

Was This Post Helpful? 3
  • +
  • -

#3 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Variable error with If Else

Posted 20 January 2011 - 05:00 AM

As said, you need to give the variables a value http://msdn.microsof...61d(VS.90).aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1