Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Array of Classes

Tags:

arrays

c#

class

I am trying to make a console game where you enter as many names as you like up to 24. To do this I have created an array of a class called PlayerData named PlayerDataAr[] which has 24 elements. It prompts the user to enter some names and those names are assigned to each element in the array which a value of string Name and bool isAlive but for some reason I can't seem to be able to access these values while I am assigning them to the player.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hG
{
    public class PlayerData
    {
        private static bool _isAlive;
        public static bool isAlive
        {
            get { return _isAlive;}
            set { _isAlive = value;}
        }

        private static string _Name;
        public static string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public PlayerData()
        {
            _isAlive = false;
            _Name = "";
        }

        public static void SetisAlive(bool a)
        {
            _isAlive = a;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string EnterName = "";

            //Array of Player Data
            PlayerData[] PlayerDataAr = new PlayerData[24];
            for (int x = 0; x < 24; x++ )
            {
                PlayerDataAr[x] = new PlayerData();
            }

            //Set up the Console
            Console.Title = "Hunger Games";
            Console.SetBufferSize(100, 42);
            Console.SetWindowSize(100, 42);

            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.Clear();

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.BackgroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("Welcome");
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Enter the names for tributes and press enter when done:");

            //Loop through collecting names
            for(int x = 0; x < 25; x++)
            {
                Console.Write("--> ");
                EnterName = Console.ReadLine();
                if (EnterName == "")
                {
                    break;
                }
                else
                {
                    //Assign Player Data
                    PlayerDataAr[x].Name = EnterName;        //Error appears here
                    PlayerDataAr[x].isAlive = true;          
                }                
            }
            Console.Clear();

            for (int x = 0; x < 24; x++)
            {

            }

            //Start Game
            while(true)
            {
                Console.ReadLine();
            }    
        }
    }

}

It returns:

Member 'hG.PlayerData.isAlive.get' cannot be accessed with an instance reference; qualify it with a type name instead.

I don't know what it is talking about it appears for both Name and isAlive. Any help would be greatly appreciated.

like image 499
Nicholas Ramsay Avatar asked Sep 04 '15 06:09

Nicholas Ramsay


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

Remove static from PlayerData:

public class PlayerData
{
    private bool _isAlive;
    public bool isAlive
    {
        get { return _isAlive;}
        set { _isAlive = value;}
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public PlayerData()
    {
        _isAlive = false;
        _Name = "";
    }

    public void SetisAlive(bool a)
    {
        _isAlive = a;
    }
}

A even better design is to use auto implemented properties:

public class PlayerData 
{
    public bool isAlive
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public PlayerData()
    {
        isAlive = false; // redundant isAlive == false by default
        Name = "";
    }

    // redundant: you can easily put isAlive = value;
    public void SetisAlive(bool value)
    {
        isAlive = value;
    }
}

static means class-based: PlayerData as a whole has a singular isAlive property value. You want different behaviour: each PlayerData instance has its own property value, that's why isAlive should be just a instance property (no static).

like image 70
Dmitry Bychenko Avatar answered Sep 22 '22 09:09

Dmitry Bychenko


You get error, because isAlive is static, which means it's not part of any instance. If you'd want to assign a value to your isAlive property you need to do this by type name:

PlayerData.isAlive = true;

But looking at your code, what you really want to do is remove static and access it through the instance reference:

private bool _isAlive;
public bool isAlive
{
    get { return _isAlive;}
    set { _isAlive = value;}
}

Then PlayerDataAr[x].isAlive = true; will work fine.

There is nice and simple explanation of static keyword here.

like image 4
michalk Avatar answered Sep 22 '22 09:09

michalk