Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a player class and I am trying to use my player class with my menu driven player program but when I call my ProcessMenuItem I keep getting the error the name 'number' does not exist in current context. Any help would be appreciated.

C#
//The MAXPLAYERS constant is the physical table size
            const Int32 MAXPLAYERS = 23;

            //Declare the player tables
            
            Player[] players = new Player[MAXPLAYERS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 playerCount = 0;

            //Main Driver
            char menuItem;
            Console.WriteLine("Welcome to the player system...\n");
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {
                ProcessMenuItem(menuItem, number, lastName, goals, assists, players, ref playerCount, MAXPLAYERS);
                menuItem = GetMenuItem();
            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //new Player(number, firstName, lastName, goals, assists)
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'C'
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
            Console.WriteLine("\nPlease pick an item:");
            Console.WriteLine("C - Create Player");
            Console.WriteLine("R - Retrive Player");
            Console.WriteLine("U - Update Player");
            Console.WriteLine("D - Delete Player");
            Console.WriteLine("L - List Players");
            Console.WriteLine("X - Exit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, Int32 number, String firstName, String lastName, Int32 goals,
            Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS, Int32 RemoveAt)
        {
            switch (menuItem)
            {
                case 'C':
                    ProcessCreate(number, firstName, lastName, goals, assists, players, ref playerCount, MAXPLAYERS);
                    break;
                case 'L':
                    ProcessList(number, firstName, lastName, goals, assists, players, ref playerCount);
                    break;
                case 'R':
                    ProcessRetrive( number, lastName, firstName, goals,  assists, players, ref playerCount, MAXPLAYERS);
                    break;
                    
                case 'U':
                    ProcessUpdate(number, firstName,lastName,goals ,assists, players, ref playerCount, MAXPLAYERS);
                    break;
                case 'D':
                    DeletePlayer(number, firstName,lastName,goals ,assists, players,ref playerCount, MAXPLAYERS, RemoveAt);
                       break;

            }
        }
Posted
Comments
Ainy Mughal 13-Nov-14 23:56pm    
did u initialized number variable before calling ProcessMenuItem???
Kiran Wilson 14-Nov-14 1:52am    
I am not able to find any initialization for number in this code Snippet.
Did you missed it..?
BillWoodruff 14-Nov-14 2:20am    
If you did not declare 'number in a scope accessible by your code that uses it: of course, it does not exist.

You are calling the ProcessMenuItemmethod,and passing in the number argument.
But you need to declare it first as

int number=0;

then call the method

ProcessMenuItem(menuItem, number, lastName, goals, assists, players, ref playerCount, MAXPLAYERS);
 
Share this answer
 
Quote:
ProcessMenuItem(menuItem, number, lastName, goals, assists, players, ref playerCount, MAXPLAYERS);
As others already noticed, you probably didn't declare the number parameter you are passing.
Please note, you are also passing less (8) parameters than expected (10) with qualifier mismatches (e.g. the method expected ref as 8th argument).
I would suggest you to change the method signature: pack so many parameters into an object and pass it to the method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900