-1

I'm still learning to code so if there are some issues, please tell me! This is for a school essay.

I'm writing a code that asks some information about a game: game name, the devs behind it, the publisher, and its cost.

The essay has multiple different tasks, like: make a program that uses get set, arrays, methods, or and so on. I made it all into a program, and I got stuck with the error

CS0120 An object reference is required for the non-static field, method, or property 'Program.gameInfoArray'

The code:

class Program
{
    //the class array that stores the games
    public gameInfo[] gameInfoArray = new gameInfo[10];

    static void Main(string[] args)
    {


        bool done = false;
        // this is where i get the issue 
        for (int i = 0; i >= gameInfoArray.Length || done == false; i++)
        {
            //asks what the game is called
            Console.WriteLine("What is the game called:");
            string gameName = Console.ReadLine();

            //asks who the devolopers are
            Console.WriteLine("Who where the devolopers behind the game:");
            string devoloper = Console.ReadLine();

            //asks who published the game
            Console.WriteLine("Who released the game:");
            string publisher = Console.ReadLine();

            //ask how much the game costs
            Console.WriteLine("How much does the game cost:");
            string cost = Console.ReadLine();

            //inputs the information in to the class array, this is also where i get the issue 
            gameInfoArray[i].gameInformationGrabber(gameName, devoloper, publisher, cost);

            //asks if the 
            Console.WriteLine("are there any more games? Y/N");
            while(true)
            {
                ConsoleKeyInfo yesOrNo = Console.ReadKey();
                if ((yesOrNo.KeyChar == 'Y') || (yesOrNo.KeyChar == 'y'))
                {
                    done = true;
                    break;
                }
                else if ((yesOrNo.KeyChar == 'N') || (yesOrNo.KeyChar == 'n'))
                {
                    break;
                }

            }
        }

    }
}

The script:

class gameInfo
{
    private string gameName;
    private string devoloper;
    private string publisher;
    private string cost;

    public void gameInformationGrabber(string game, string dev, string publisher, string cost)
    {
        theGameName = game;
        theDevs = dev;
        thePublisher = publisher;
        theCost = cost;

    }
    public string theGameName
    {
        get { return gameName; }
        set { gameName = value; }
    }

    public string theDevs
    {
        get { return devoloper; }
        set { devoloper = value; }
    }

    public string thePublisher
    {
        get { return publisher; }
        set { publisher = value; }

    }
    public string theCost
    {
        get { return cost; }
        set { cost = value; }

    }

}

Thanks a ton in advance. Sorry if I messed up big time somewhere in the code.

spagmon
  • 3
  • 2
  • 4
    Possible duplicate of [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) The very first thing you should do when you come here is to do a basic search. In this case, a simple search on the error message finds the answer for you. – Ken White Oct 24 '19 at 23:30
  • 3
    public static gameInfo[] ... – Hans Passant Oct 24 '19 at 23:30

1 Answers1

1

You've got two comment, it is perfect solution for you. I hope you spend a time to get a basic concept from it. However, this will help you.

1. Static only

This is a solution mentioned comment.

class Program
{
    // Add static keyword. Because Main() method is static. 
    // So, every variable inside it should be static.
    // public gameInfo[] gameInfoArray = new gameInfo[10];
    public static gameInfo[] gameInfoArray = new gameInfo[10];

    static void Main(string[] args)
    {
       ......
    }
}

2. Work with local variable

class Program
{    
    //public gameInfo[] gameInfoArray = new gameInfo[10];    

    static void Main(string[] args)
    {
       // Declare as local variable.
       // In static method will now complain using local variable.
       gameInfo[] gameInfoArray = new gameInfo[10];
       ......
    }
}

I've tried to run this code, plase don't forget initialize gameInfoArray to prevent null reference exception. you may need this before entering for loop,

for (int index = 0; index < gameInfoArray.Length; index++)
{
   gameInfoArray[index] = new gameInfo();
}
jornathan
  • 646
  • 5
  • 13
  • Thanks! The code works perfectly now! I put the intializer of `gameInfoArray` in the for loop of the main program, thats where i thought it should be. – spagmon Oct 25 '19 at 11:43