Ben Dorett
Ben Dorett

Reputation: 13

How do I access this list in C# in other functions?

I am in early stages of creating a game in which people bet on horses. I need to access the 'HorseList' in the 'BetOnARace' function. This was my attempt but there is an error message near the end for the word 'Main' in the class 'Program' It says that it 'has the wrong signature to be an entry point'.

class RunGame
{

    public void StartMenu(List<Horse> HorseList)
    {
        Console.WriteLine("**********    MENU     **********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. LOAD GAME");
        Console.WriteLine("2. NEW GAME");
        Console.WriteLine("3. EXIT GAME");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        if (choice == "3")
        {
            Environment.Exit(0);
        }
        if (choice == "1" || choice == "2")
        {
            StartingBalance(HorseList);
        }
    }

    public void StartingBalance(List<Horse> HorseList)
    {
        int balance;
        Console.WriteLine("****    STARTING BALANCE     ****");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. DEFAULT (£500)");
        Console.WriteLine("2. CUSTOM");
        Console.WriteLine("3. BACK TO MAIN MENU");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        if (choice == "1")
        {
            balance = 500;
            GameMenu(HorseList);
        }
        if (choice == "2")
        {
            Console.WriteLine("Enter balance: ");
            Console.Write("£");
            balance = Convert.ToInt32(Console.ReadLine());
            if (balance <= 0)
            {
                Console.WriteLine("Balance must start above £0");
                Console.WriteLine("Enter balance: ");
                Console.Write("£");
                balance = Convert.ToInt32(Console.ReadLine());
                GameMenu(HorseList);
            }
            else
            {
                GameMenu(HorseList);
            }
        }
        if (choice == "3")
        {
            RunGame rg = new RunGame();
            rg.Run(HorseList);
        }
    }


    public void GameMenu(List<Horse> HorseList)
    {
        Console.WriteLine("**********    MENU     **********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. BET ON A RACE");
        Console.WriteLine("2. VISIT STABLES");
        Console.WriteLine("3. EXIT TO START MENU");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        switch (choice)
        {
            case "1":
                BetOnRace(HorseList);
                break;
            case "2":
                DisplayHorses();
                break;
            case "3":
                Console.WriteLine("GAME PROGRESS SAVED");
                RunGame rg = new RunGame();
                rg.Run(HorseList);
                break;
        }
    }

    public void DisplayHorses()
    {
        List<Horse> HorseList = new List<Horse>();
        HorseList.Add(new Horse("Purple Haze", 2, 0, 0));
        HorseList.Add(new Horse("Druggo", 3, 0, 0));
        HorseList.Add(new Horse("Mr Crowley", 7, 0, 0));
        HorseList.Add(new Horse("I Am Monky", 10, 0, 0));
        HorseList.Add(new Horse("Roy", 12, 0, 0));
        HorseList.Add(new Horse("Egg Mayo", 14, 0, 0));
        HorseList.Add(new Horse("Crash My Horse Into A Bridge", 17, 0, 0));
        HorseList.Add(new Horse("Chubungundy", 20, 0, 0));
        HorseList.Add(new Horse("Uncle Barry", 22, 0, 0));
        HorseList.Add(new Horse("Widgetygrub", 2, 0, 0));
        HorseList.Add(new Horse("Paid In Raisins", 8, 0, 0));
        HorseList.Add(new Horse("Crayzee", 15, 0, 0));
        HorseList.Add(new Horse("Camster", 25, 0, 0));
        HorseList.Add(new Horse("Cheese N Peas", 18, 0, 0));
        HorseList.Add(new Horse("Master Oogway", 20, 0, 0));
        HorseList.Add(new Horse("Danny DeVito", 11, 0, 0));
        HorseList.Add(new Horse("Yobungus", 4, 0, 0));
        HorseList.Add(new Horse("Hot Cheetos", 14, 0, 0));
        HorseList.Add(new Horse("Bobby", 22, 0, 0));
        HorseList.Add(new Horse("This Horse Will Win", 50, 0, 0));

        Console.WriteLine("********    STABLES     *********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. DISPLAY HORSES");
        Console.WriteLine("2. BUY HORSE");
        Console.WriteLine("3. SELL HORSE");
        Console.WriteLine("4. BACK");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        switch (choice)
        {
            case "1":
                foreach (var horse in HorseList)
                {
                    Console.WriteLine("Name of Horse: {0}, Odds: {1}/1, Races: {2}, Wins: {3}", horse.GetHorseName, horse.GetOdds, horse.GetRaces, horse.GetWins);
                }
                DisplayHorses();
                Console.ReadLine();
                break;
            case "2":
                break;
            case "3":
                break;
            case "4":
                GameMenu(HorseList);
                break;
        }
    }

    public void BetOnRace(List<Horse> HorseList)
    {
        Random rnd = new Random();
        HorseList.OrderBy(x => rnd.Next()).Take(5);
    }

    public void Run(List<Horse> HorseList)
    {
        StartMenu(HorseList);
    }
}
class Program
{
    static void Main(string[] args, List<Horse>HorseList)
    {
        RunGame rg = new RunGame();
        rg.Run(HorseList);
    }
}
}

Upvotes: 0

Views: 74

Answers (1)

Jamiec
Jamiec

Reputation: 136164

The error is due to this line:

static void Main(string[] args, List<Horse>HorseList)

You can't change the signature of that Main method for a console app, it needs to just have a single parameter of type string[] (commonly named args but can be named anything).

But, I can see what you're trying to do.

You have a class RunGame and what you should do is twofold

  1. Add a constructor on this class which takes yor list of horses (or, if you prefer actually generate the list inside the constructor)
  2. Store that list in a private property of the class

Then you can use it throughout, without having to pass it through every method of that class.

class RunGame
{
     private readonly List<Horse> horsesList;

    public RunGame()
    {
         this.horseList = new List<Horse>
         {
           new Horse("Purple Haze", 2, 0, 0),
           new Horse("Druggo", 3, 0, 0),
           // .. etc .. //
         };
    }

    public void DisplayHorses()
    {
        foreach(var horse in horsesList)
        {
            // TODO: display details of horse
        }
    }
}

Upvotes: 3

Related Questions