How to instantiate a class with predefined and immutable parameters C#

Asked

Viewed 25 times

0

How to instantiate this class jogo?

public class Jogo
{
        public string Name { get; set; }
        public string Genre { get; }
        public string Plataform { get; }

        public Jogo(string name, string genre, string plataform)
        {
            Name = name;
            Genre = genre;
            Plataform = plataform;
        }
}

with the parameters 'Genre' and 'Plataform' of a predefined and immutable list?

I thought about enum:

enum Genre { Ação, Plataforma, FPS, Aventura, Corrida };

// ou uma lista
var plataform = new List<string>(){ "PS4", "PS5", "Xbox One", "Xbox Series X" };

But I couldn’t instantiate

var game = new Jogo("God  of War", <item da List ou Enum>, <item da List ou Enum>);


// eu tentei mudar a propriedade na class 'Jogo'
// com List:
        public List<string> Genre { get; }
        public List<string> Plataform { get; }
// ou com enum
        public Genre category{ get; }
        public System Plataform { get; }

How to instantiate?

With one class for 'Platform' and another for 'Genre'... or an Enum within 'Game'? What is the best implementation?

  • I don’t understand? ...

  • I have the Game class, at the time of instantiation I wanted to limit the options of the second parameter 'Enre', not to put anything. That is: new game("string_qualquer", Genre.Ação, Plataform.PS4); So I avoid mistakes of the new game type("string_qualquer", "banana", "pessego");

  • So don’t use strings for the type of the field, if what you want is a class or an Enum. C# allows you to define the type of the field. Use as static, non-dynamic typing language.

  • tenteus enum?.

  • I’m trying again. Damn it!

  • I will put the solution, in case anyone has the same doubt.

Show 1 more comment

2 answers

2


If you have a defined and immutable list use enum, example:

public enum Genres
{    
    Acao, 
    Plataforma, 
    FPS, 
    Aventura, 
    Corrida
}

public enum Plataforms
{
    PS4, 
    PS5, 
    XboxOne, 
    XboxSeriesX
}

and finally:

public class Jogo
{
        public string Name { get; set; }
        public Genres Genre { get; }
        public Plataforms Plataform { get; }

        public Jogo(string name, Genres genre, Plataforms plataform)
        {
            Name = name;
            Genre = genre;
            Plataform = plataform;
        }
}

and in its instance:

var jogo = new Jogo("Jogo 1", Genres.Acao, Plataforms.PS4);

0

I resolved as follows: creating a class Jogo

public class Jogo
    {

        public string Name { get; set; }
        public Genre Category { get; }
        public string Plataform { get; set; }

        public Jogo(string name, Genre category, string plataform)
        {
            Name = name;
            Category = category;
            Plataform = plataform;
        }
    }

I created an Enum Genre.cs

{
    public enum Genre
    {
        Ação, Aventura, Corrida, Luta
    }
}

and so I established program > main

      var game = new Jogo("gow", Genre.Ação, "PS4");
      Console.WriteLine(game.Category);
     //# Ação

Browser other questions tagged

You are not signed in. Login or sign up in order to post.