1

I have to make a program which generates lotto ticket then checks to see if there is a winner. I am having problems even calling my method which is only a simple menu function.

This is the error:

Error 1 An object reference is required for the non-static field, method, or property

I am not sure where i have went wrong but i think its to do with visibility.

I have tried changing some functions to public static but that has not worked.

class Program
{

    bool quitFlag = false;
    SortedSet<int> winningNumbers = new SortedSet<int>();
    List<CTicket> ticketList = new List<CTicket>();

    static void Main(string[] args)
    {

        DoMenu();   
    }

    public static void GenerateTickets(List<CTicket> l)
    {

        Random Rand = new Random();

        for(int i=0; i <1000; i++)
        {
            SortedSet<int> num = new SortedSet<int>();

            do
            {

                num.Add(Convert.ToInt32(Rand.Next(1,42)));
            }
            while(num.Count <=6);

            CTicket ticket = new CTicket(i,num);
            l.Add(ticket);
            ticket.printTicket();
        }
    }

    public static void DrawWinningNumbers(SortedSet<int> winningNumbers)
    {

        for(int i=1; i <6; i++)
        {

            Random Rand = new Random();
            winningNumbers.Add(Convert.ToInt32(Rand.Next(1,42)));

            while(winningNumbers.Count <=6);
            Console.Write("The winning numbers are: {");

            foreach (int x in winningNumbers)
            {
                Console.Write(x + ", ");
            }

            Console.WriteLine();
        }
    }

    void DoQuit()
    {

        quitFlag = true;
    }

    int ShowMenu()
    {

        int option;

        Console.WriteLine("\t-------------------------------------------------\n");
        Console.WriteLine( "\t\t\t Queue Application\n\n");
        Console.WriteLine("\t1. \tGenerate tickets\n");
        Console.WriteLine("\t2. \tDisplay all tickets\n");
        Console.WriteLine("\t3. \tGenerate Winning Numbers\n");
        Console.WriteLine("\t4. \tPrint queue\n");
        Console.WriteLine("\t5. \tReverse queue\n");
        Console.WriteLine("\t6. \tExit\n");

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

        Console.WriteLine();
        Console.WriteLine();
        return option;
    }

    public static void DoMenu()
    {

        do
        {
            int option;

            option = ShowMenu();

            switch (option)
            {
                case 1:
                    GenerateTickets(ticketList);
                    //system("cls");
                    break;
                case 2:
                    //system("cls");
                    break;
                case 3:
                    DrawWinningNumbers(winningNumbers);
                    //system("cls");
                    break;
                case 4:
                    break;
                case 5:
                    //system("cls");
                    break;
                case 6:
                    DoQuit();
                    break;
                default:
                    //system("cls");
                    Console.WriteLine("invalid option!\n");
                    break;
            }
        } while (quitFlag != true);
    }
}
Amitd
  • 4,769
  • 8
  • 56
  • 82
  • ive uploaded the code there. Ive tried making the DoMenu function public static void but it didnt help – stephen reilly Feb 04 '16 at 17:50
  • 4
    Please don't link to an image of your code. Insert the code into your question and use the formatting buttons `{}` to format it as code. – Eric J. Feb 04 '16 at 17:51
  • Please put your code in the question. – SLaks Feb 04 '16 at 17:51
  • 4
    Also please do search for the error, you will get tons of forum which explains the issue. – Viru Feb 04 '16 at 17:52
  • A quick review http://stackoverflow.com/questions/4124102/whats-a-static-method-in-c – Steve Feb 04 '16 at 17:55
  • Im sorry i wasnt aware of how to do that. And i have searched everywhere i just cant spot it in my program – stephen reilly Feb 04 '16 at 17:58
  • 1
    If you don't know the difference between static and instance properties/methods, you're getting ahead of yourself. Stop. Go buy a copy of CLR Via C#. Read it. It shouldn't take you more than a day or two. –  Feb 04 '16 at 18:03

1 Answers1

0

The error should be clear - you are accessing non-static items (quitFlag, winningNumbers, ticketList) from a static method.

Either change the items above to static (if they should be shared by ALL instances of the Program class), or make the methos non-static and call it on an instance of Program.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • thanks for your response. I changed all the items to static and i still have an error saying "an object reference is required for the non-static field,method or property of DoMenu. I also tried to call the function by Program.DoMenu() if that what you mean by calling as an instance – stephen reilly Feb 04 '16 at 18:11
  • Do you know how to put in breakpoints? My guess at this point is because ShowMenu() isn't static, it's still freaking out. You can try setting ShowMenu to static, or you can put a breakpoint in at the beginning of "DoMenu" then step through until you find the line that breaks, and address it. – Aaron Feb 04 '16 at 18:17
  • If `DoMenu` is not static then you'd need an instance of `Program` to call it - `Program.DoMenu()` would not work. – D Stanley Feb 04 '16 at 18:19