Reputation: 439
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#.
Upvotes: 0
Views: 2404
Reputation: 1039508
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()
{
}
Upvotes: 4