How to assign a class to one of another Class

Asked

Viewed 41 times

-1

In a program I created a class called Player and in it assigns various values, among them there is the value of Itens.

In addition to this class, there is another class called Itens with different attributes. I would like to know how to assign the class Itens at value Itens class Player.

That is all values of the class Itens would go for the attribute Itens class Player.

Here is my code:

    using System;
using System.Collections.Generic;


public class Player
{
    public string nome;
    public int xp;
    public int gold;
    public Item();

}

public class Item
{
    public string nome;
    public int iD;
    public int preco;
}
public class LP2_Trabalho
{

    static List<Player> Players = new List<Player>();

    static void Main(string[] args)
    {
        
        bool MostrarMenu = true;
        while (MostrarMenu)
        {
            MostrarMenu = MenuPrincipal();
        }
    }


    private static bool MenuPrincipal()
    {

        Console.Clear();
        Console.WriteLine("Entre com '1' para criar um jogador (necessário para entrar na loja).");
        Console.WriteLine("Entre com '2' para checar as informações de todos os jogadores criados.");
        Console.WriteLine("Entre com '3' para olhar o seu inventário.");
        Console.WriteLine("Entre com '4' para entrar na loja.");
        Console.WriteLine("Entre com '4' para encerrar o programa.");

        switch (Console.ReadLine())
        {
            case "1":

                Console.Clear();  
                
                Console.Write("Digite o nome do seu jogador: ");
                var np = Console.ReadLine();
              
                Console.Write("Digite a quantidade de XP (experiência): ");
                var xpP = Console.ReadLine();
               
                Random randNum = new Random();
                var dinDin = randNum.Next(50, 200);


                Players.Add(new Player { nome = np, xp = Convert.ToInt32(xpP), gold = dinDin});
                Console.Clear();

                Console.WriteLine("Jogador criado com sucesso");
                Console.WriteLine("");
                Console.WriteLine("Verifique o seu gold (gerado de forma aleatoria): " + dinDin);
                Console.WriteLine("Tecle 'ENTER' para continuar...");
                Console.ReadLine();
                return true;

            case "2":
                Console.Clear();
                for (int i = 0; i < Players.Count; i++)
                {

                    Console.WriteLine("");
                    Console.WriteLine("Jogadores:");            
                    Console.WriteLine("Nome : " + Players[i].nome);
                    Console.WriteLine("XP : " + Players[i].xp);
                    Console.WriteLine("Gold : " + Players[i].gold);                 
                    Console.WriteLine("");
                }

                Console.WriteLine("");
                Console.Write("Pressione qualquer tecla para sair...");
                Console.ReadKey();
                return true;
            
            case "3":
                return true;

            case "4":
                return false;

            default:
                return true;

        }
    }


}
  • PLEASE! If my question is not good for the site tell me what to do! I’ve read the survival guide and the other article showing why my question is not in good quality. Also I already tried to google my problem before coming here. Please don’t close my question :(

  • Hello Carlos. Try to show us your class Itens that you said in the question. You quoted that there is this class, but you don’t show us how it is created. :)

  • @Pedropaulo sorry about that. I’ve fixed the problems of the question.

1 answer

1


From what I understand from your question, you are creating an algorithm (probably for study) to simulate a game. You have a player (Player) who may have one or several items. For this you need your class Player have a collection of items.

public class Player
{
    public string Nome { get; set; }
    public int Xp { get; set; }
    public int Gold { get; set; }
    public List<Item> Itens { get; set; } = new List<Item>();
}

public class Item
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public decimal Preco { get; set; }
}

With this in hand, what you need is to create a new object Item and add to player item list.

For example, in action 1, where you create your Player, can also create an item and add to the list.

Player player = new Player();

player.Nome = np;
player.Xp = Convert.ToInt32(xpP);
player.gold = dinDin;

// Aqui criamos um novo objeto item e definimos os valores dos seus campos
Item item = new Item();
item.Id = 10;
item.Nome = "Machado de pedra";
item.Preco = 100;

// Aqui associamos esse nosso novo item aos itens do nosso player
player.Itens.Add(item);

// Aqui adicionamos o nosso player na lista de players do jogo
Players.Add(player);

I hope I’ve helped!

  • 1

    Perfect. Thank you very much! That’s exactly what you said. It’s a school job and I had already picked up enough doubts with the teacher and was afraid of being too boring kkk. Thank you!

  • Great! If the answer helped you solve the problem, can you vote as the answer you accept? So we were able to show other people that this answer solved their problem. Thank you!

  • I didn’t know that function. I put your answer!

Browser other questions tagged

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