astrimbu
astrimbu

Reputation: 61

Variables reset to zero when I call a method

I want choice == 1 to only be able to be selected five times, so I initialized a variable firstClass = 0, then set up a do-while for firstClass < 5. I included firstClass++ in my do-while to act as a counter. However, I think firstClass is re-initializing every time I call the method CheckIn(). How can I prevent this from happening? Thanks in advance.

using System;

namespace Assignment7
{
    class Plane
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Airline Reservation System.");
            Console.WriteLine("Where would you like to sit?\n");
            Console.WriteLine("Enter 1 for First Class.");
            Console.WriteLine("Enter 2 for Economy.");
            CheckIn();
        }

        public static void CheckIn()
        {
            int choice = Convert.ToInt32(Console.ReadLine());
            int firstClass = 0;
            int economy = 0;

            if (choice == 1)
            {
                do
                {
                    Console.WriteLine("You have chosen a First Class seat.");
                    firstClass++;
                    CheckIn();
                } while (firstClass < 5);
            }
            else if (choice == 2)
            {
                do
                {
                    Console.WriteLine("You have chosen an Economy seat.");
                    economy++;
                    CheckIn();
                } while (economy < 5);
            }
            else
            {
                Console.WriteLine("That does not compute.");
                CheckIn();
            }
        }
    }
}

Upvotes: 0

Views: 5501

Answers (3)

Joseph
Joseph

Reputation: 25513

You need to take any exit condition out of your method and put it outside, either by making a new method or putting it in the one that's calling it already.

For example you could do something like:

using System;

namespace Assignment7
{
    class Plane
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Airline Reservation System.");
            Console.WriteLine("Where would you like to sit?\n");
            Console.WriteLine("Enter 1 for First Class.");
            Console.WriteLine("Enter 2 for Economy.");
            CheckIn(0, 0);
        }

        public static void CheckIn(int firstClassSeatsTaken, int economySeatsTaken)
        {
            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                do
                {
                    Console.WriteLine("You have chosen a First Class seat.");
                    firstClass++;
                    CheckIn(firstClassSeatsTaken, economySeatsTaken);
                } while (firstClass < 5);
            }
            else if (choice == 2)
            {
                do
                {
                    Console.WriteLine("You have chosen an Economy seat.");
                    economy++;
                    CheckIn(firstClassSeatsTaken, economySeatsTaken);
                } while (economy < 5);
            }
            else
            {
                Console.WriteLine("That does not compute.");
                CheckIn(firstClassSeatsTaken, economySeatsTaken);
            }
        }
    }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

That is entirely normal. If you want the variable to exist outside the method, you must declare it outside a method, as a "field". Simply move the:

int firstClass = 0;

outside the method, adding the static modifier (in this case):

static int firstClass = 0;

Note also that this by itself is not thread-safe; if threading is an issue (for example, ASP.NET) then use int newValue = Interlocked.Increment(ref firstClass);. I only mention this because in the general case static data should consider threading, but I suspect it isn't an issue in your case (a console exe).

Upvotes: 2

Jason
Jason

Reputation: 1226

The firstClass variable is method scoped. Every time the method is called the variable is reinitialized. To have firstClass become an ongoing counter it needs to be outside the method, within the class.

Upvotes: 1

Related Questions