0

So, I have this class with the setters and getters and a constructor.

namespace Ficha04
{
    class Personagem
    {
        private string nome;
        private int vida;
        private int mana;
        private int estamina;
        private int moral;
        private int forca;
        private int inteligencia;
        private int destreza;

        public string Nome
        {
            get
            {
                return nome;
            }

            set
            {
                if (value.Length > 0)
                {
                    nome = value;
                }
            }
        }

        public int Vida
        {
            get
            {
                return vida;
            }

            set
            {
                if (value < 0)
                {
                    vida = 0;
                }
                else
                {
                    vida = value;
                }
            }
        }

        public int Mana
        {
            get
            {
                return mana;
            }

            set
            {
                if (value < 0)
                {
                    mana = 0;
                }
                else
                {
                    mana = value;
                }

            }
        }

        public int Estamina
        {
            get
            {
                return estamina;
            }

            set
            {
                if (value < 0)
                {
                    estamina = 0;
                }
                else
                {
                    estamina = value;
                }
            }
        }

        public int Moral
        {
            get
            {
                return moral;
            }

            set
            {
                moral = value;
            }
        }

        public int Forca
        {
            get
            {
                return forca;
            }

            set
            {
                if (value < 10)
                {
                    value = 10;
                }
                else
                {
                    forca = value;
                }
            }
        }

        public int Inteligencia
        {
            get
            {
                return inteligencia;
            }

            set
            {
                if (value < 25)
                {
                    value = 25;
                }
                else
                {
                    inteligencia = value;
                }
            }
        }

        public int Destreza
        {
            get
            {
                return destreza;
            }

            set
            {
                if (value < 10)
                {
                    value = 10;
                }
                else
                {
                    destreza = value;
                }
            }
        }

        public Personagem(string nome, int vida, int mana, int estamina, int moral, int forca, int inteligencia, int destreza)
        {
            Nome = nome;
            Vida = vida;
            Mana = mana;
            Estamina = estamina;
            Moral = moral;
            Forca = forca;
            Inteligencia = inteligencia;
            Destreza = destreza;
        }
    }
}

And Im trying to create an object from this class:

private void btnInsere_Personagem_Click(object sender, EventArgs e)
{

    Personagem persona1 = new Personagem()
    {
        Nome = textBox_Nome.Text,
        Vida = Convert.ToInt32(upDown_Vida.Value),
        Mana = Convert.ToInt32(upDown_Mana.Value),
        Estamina = Convert.ToInt32(upDown_Estamina.Value),
        Moral = Convert.ToInt32(upDown_Moral.Value),
        Forca = Convert.ToInt32(upDown_Forca.Value),
        Inteligencia = Convert.ToInt32(upDown_Inteligencia.Value),
        Destreza = Convert.ToInt32(upDown_Destreza.Value),
    };
}

And I get this error: "There is no argument given that corresponds to the required formal parameter 'nome' of 'Personagem.Personagem(string, int, int, int, int, int, int, int)'". I changed to nome = textBox_Nome.Text, persona1.Nome = textBox_Nome.Text, persona1.nome = textBox_Nome.Text,,... and I still cant get it to work. What am I missing ? Sorry about something, Im new to c#.

4
  • Did you provide the text in the textbox? Commented Mar 12, 2017 at 15:51
  • The idea is to run the program, input the text and create the object with the text that is inputted in the textbox. There is no value there until the "user" inserts one. Commented Mar 12, 2017 at 15:53
  • Sorry, I think I made as mistake here Commented Mar 12, 2017 at 16:01
  • No problem, thank you anyway. Commented Mar 12, 2017 at 16:02

1 Answer 1

4

Your class doesn't have a default constructor, so you could use the one you defined and pass the parameters to it:

Personagem persona1 = new Personagem(
    textBox_Nome.Text,
    Convert.ToInt32(upDown_Vida.Value),
    Convert.ToInt32(upDown_Mana.Value),
    Convert.ToInt32(upDown_Estamina.Value),
    Convert.ToInt32(upDown_Moral.Value),
    Convert.ToInt32(upDown_Forca.Value),
    Convert.ToInt32(upDown_Inteligencia.Value),
    Convert.ToInt32(upDown_Destreza.Value)
);

Alternatively if you want to use the properties initializer syntax, you could define a default constructor to your class:

public Personagem()
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

isnt this my deafult constructor ? "public Personagem(string nome, int vida, int mana, int estamina, int moral, int forca, int inteligencia, int destreza)..." ? Im kinda lost now
No, this is not a default constructor. In .NET a default constructor is called a constructor that doesn't take any parameters. Yours take 8 parameters. A default constructor looks like this: public class Personagem() {}. So you could either remove your custom constructor or add a default constructor if you want to use the class initializer syntax. Otherwise it's also fine to have a custom constructor taking parameters, you just need to make sure that you supply them when you are creating an instance of your class as shown in my answer.
Ho ok, got it. Thank you very much !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.