Good to all and to all. I'm new to the world of C# and I have a question.
I am doing a practice related to the role character file.
The practice asks me to create a character and manage that data to save it in a text file.
Summarize what I have:
Form1 with all the fields to fill and functions.
Character class that creates and manages the data to move it to the file. Once the data is filled in, we click on the save button that contains the following code:
private void btnGuardar_Click(object sender, EventArgs e)
{
//Pre requisitos
if (obtenerNombre() == "")
{
MessageBox.Show("Por favor, introduzca un nombre");
}
if (numPuntos == 0)
{
//Al pulsar en el boton de guardar, creamos el personaje y lo pasamos al fichero
String[] caracteristicas = new String[6];
String[] mochila = new String[16];
String[] habilidades = new String[6];
Personaje personaje = new Personaje(obtenerNombre(), recogerSexo(), recogerRaza(), obtenerProfesion(), obtenerEspecialidad());
//Vamos recogiendo los datos restantes
//Comenzamos por las caracteristicas
caracteristicas[0] = numbVida.Text.ToString();
caracteristicas[1] = numbMana.Text.ToString();
caracteristicas[2] = numbAD.Text.ToString();
caracteristicas[3] = numbDef.Text.ToString();
caracteristicas[4] = numbSpeed.Text.ToString();
caracteristicas[5] = numbRegen.Text.ToString();
personaje.setCaracteristicas(caracteristicas);
//rellenamos la mochila
mochila[0] = chkItem1.Checked.ToString();
mochila[1] = chkItem2.Checked.ToString();
mochila[2] = chkItem3.Checked.ToString();
mochila[3] = chkItem4.Checked.ToString();
mochila[4] = chkItem5.Checked.ToString();
mochila[5] = chkItem6.Checked.ToString();
mochila[6] = chkItem7.Checked.ToString();
mochila[7] = chkItem8.Checked.ToString();
mochila[8] = chkItem9.Checked.ToString();
mochila[9] = chkItem10.Checked.ToString();
mochila[10] = chkItem11.Checked.ToString();
mochila[11] = chkItem12.Checked.ToString();
mochila[12] = chkItem13.Checked.ToString();
mochila[13] = chkItem14.Checked.ToString();
mochila[14] = chkItem15.Checked.ToString();
mochila[15] = chkItem16.Checked.ToString();
personaje.setMochila(mochila);
//rellenamos las habilidades
habilidades[0] = numDeterminacion.Text.ToString();
habilidades[1] = numJusticia.Text.ToString();
habilidades[2] = numValor.Text.ToString();
habilidades[3] = numSabiduria.Text.ToString();
habilidades[4] = numEscudo.Text.ToString();
habilidades[5] = numMasacre.Text.ToString();
personaje.setHabilidades(habilidades);
//hacemos el toString para pasarlo a un archivo
personaje.ToString();
personaje.ToFile();
}
else
{
MessageBox.Show("Debe poner los puntos en las caracteristicas");
}
}
This code creates an object of class characters:
class Personaje
{
//Variables
private String[] habilidades;
private String[] caracteristicas;
private String[] mochila;
private String nombre;
private String raza;
private String sexo;
private String profesion;
private String especialidad;
private String cadenaPersonaje;
//Constructor
public Personaje(String nombrePJ, String sexoPJ, String razaPJ, String profesionPJ, String especialidadPJ) {
nombre = nombrePJ;
sexo = sexoPJ;
raza = razaPJ;
profesion = profesionPJ;
especialidad = especialidadPJ;
}
public string[] getHabilidades()
{
return habilidades;
}
public void setHabilidades(String[] skills)
{
habilidades = skills;
}
public string[] getMochila()
{
return mochila;
}
public void setMochila(String[] inventory)
{
mochila = inventory;
}
public string[] getCaracteristicas()
{
return caracteristicas;
}
public void setCaracteristicas(String[] caracts)
{
caracteristicas = caracts;
}
public void toString()
{
cadenaPersonaje = "";
String delimitador = " | ";
//vamos agregando los campos
cadenaPersonaje += nombre + delimitador + sexo + delimitador + profesion + delimitador + especialidad + delimitador;
//creamos los bucles para recorrer los arrays
for (int i = 0; i < caracteristicas.Length; i++)
{
cadenaPersonaje += caracteristicas[i] + delimitador;
}
for (int j = 0; j < mochila.Length; j++)
{
cadenaPersonaje += mochila[j] + delimitador;
}
for (int k = 0; k < habilidades.Length; k++)
{
cadenaPersonaje += habilidades[k] + delimitador;
}
}
public String getCadena()
{
return cadenaPersonaje;
}
public void ToFile()
{
//pasamos todo al archivo
System.IO.StreamWriter file = new System.IO.StreamWriter(@"dataFiles/Hoja.txt",true);
file.WriteLine(cadenaPersonaje);
file.Close();
}
}
The problem is the following:
It finds the file for me but it doesn't write me anything . The toString method may be wrong.
Does anyone know why it doesn't write me to the file?
Thanks in advance.
It doesn't find anything in the file because you're writing an empty string to it. First of all, you should override the method
ToString()
inherited from object in the classPersonaje
. You're inventing a new methodtoString()
that you're not even calling in the button's event handler.In the method it
ToFile()
would callToString()
and not outside, as you are doing.And finally, in the button's event handler you would only need a
personaje.ToFile()
without previously callingToString()
.