-1

Hey so my code isn't recognizing that I've got created a constructor and it isn't recognizing my string variables. I've tried to figure out the issue but I just couldn't, so I'm here asking for advice. I've added my game class to it to help debug it a bit more.

namespace Mas
{
    class Game
    {
        private string _Title;
        private string _Category;
        public int _MinimumAge;
        private string _NumberofPlayers;
        private string title;
        private string category;
        private string numberOfPlayers;

        public Game(string Title, string Category, int MinimumAge, string NumberOfPlayers)
        {
            _Title = Title;
            _Category = Category;


            if (MinimumAge >= 0)
            {
                _MinimumAge = MinimumAge;
            }
            else
            {
                throw new Exception("Minimum age is 0 please state a higher age");
            }
            _NumberofPlayers = NumberOfPlayers;
        }

       
        public string Title
        {
            get { return _Title; }
            
        }
        public string Category
        {
            get { return _Category; }

        }
        public int MinimunAge
        {
            get { return _MinimumAge; }

        }
        public string NumberofPlayers
        {
            get { return _NumberofPlayers; }

        }

        public override string ToString()
        {
            return Title.PadRight(10) + Category.ToString().PadRight(5) + NumberofPlayers;
        }


    }
}
Bruno Casarotti
  • 623
  • 8
  • 23
  • 1
    Can you provide the full class? It isn't clear if the code above the constructor is just loose in the file or what. – Retired Ninja Aug 27 '20 at 23:47
  • 1
    1) You didn't close the if statement 2) C# has CSV reader libraries rather than relying on `Split` function – OneCricketeer Aug 27 '20 at 23:48
  • 1
    Edit it into your question. – Retired Ninja Aug 28 '20 at 00:01
  • https://paste.mod.gg/cosetowuyi.cs – Hashim Abdi Aug 28 '20 at 00:02
  • What is the compiler error? – mjwills Aug 28 '20 at 00:10
  • ```private string _NumberofPlayers; private string numberOfPlayers;``` One of those must be removed. There is no point having it there twice. In fact there is no need for _either_ of them (read https://stackoverflow.com/a/3917886/34092 under 'C# 6.0 adds readonly auto properties'). – mjwills Aug 28 '20 at 00:10
  • The edit makes the code very different from what you originally had. Which one is causing the issue? It appeared all the code was meant to be in one class, but if that is not the case then you'll need to work on a [mcve] and if you have compile errors then copy/paste those into the question too. – Retired Ninja Aug 28 '20 at 00:13
  • `string Title, string Category, int MinimumAge, string NumberOfPlayers` Change all of those to have a lower case letter as their first letter. Having capital letters at the start of parameter names is not idiomatic C#. – mjwills Aug 28 '20 at 00:14
  • You need to include the error messages in your question – tgdavies Aug 28 '20 at 12:46

2 Answers2

1

You missed a } on the first if Also your constructor requires 4 arguments:

public Game(string Title, string Category, int MinimumAge, string NumberOfPlayers)

and you are passing just 3 when calling it:

gamelist.Add(new Game(Title, Category, NumberOfPlayers));

Try to post here your entire class.

UPDATE:

public class Game
{
    private string _category;
    private int _minimumAge;
    private string _numberofPlayers;
    private string _title;

    public Game(string title, string category, int minimumAge, string numberOfPlayers)
    {
        _title = title;
        _category = category;


        if (minimumAge >= 0)
        {
            _minimumAge = minimumAge;
        }
        else
        {
            throw new Exception("Minimum age is 0 please state a higher age");
        }

        _numberofPlayers = numberOfPlayers;
    }


    public string Title => _title;

    public string Category => _category;

    public int MinimunAge => _minimumAge;

    public string NumberofPlayers => _numberofPlayers;

    public override string ToString()
    {
        return Title.PadRight(10) + Category.PadRight(5) + NumberofPlayers;
    }
}
Bruno Casarotti
  • 623
  • 8
  • 23
  • https://paste.mod.gg/cosetowuyi.cs – Hashim Abdi Aug 28 '20 at 00:02
  • I setted the constructor arguments to camelCase to avoid mismatching @HashimAbdi could you check that? – Bruno Casarotti Aug 28 '20 at 00:15
  • Also there is a private string title, and a title in the constructor as a parameter, i would change title in the constructor to something else, most common is pTitle, but thats just nitpicking, also sidequestion does it even Compile? i remember the error when the class is just "class" and the fields are public for example isnt it more accessable than the class itself? – Nico Walsemann Aug 28 '20 at 14:01
  • 1
    @NicoWalsemann I changed it a bit, it was late in the night when I coded it, I think it is more clear now – Bruno Casarotti Aug 28 '20 at 14:48
-3

Maybe you need to change declaration your variable "Title" to "_Title". Because your variable hasn't underline and your variable.