I have 3 classes, the first one is an Abstract Class called Equipos with a ToString() abstract method, the second one is called Camiones which overrides the ToString() and shows the attributes "marca" and "dominio", the third one is called Gruas and is the children of the second one (Camiones), and it also has the ToString() method and uses override to add more data to it, including the one from the second class.
Here's the class Camiones:
public class Camiones : Equipos
{
#region atributos
protected string marca;
protected string dominio;
#endregion
#region constructores
public string Marca
{
get { return marca; }
set
{
marca = value;
}
}
public string Dominio
{
get { return dominio; }
set
{
dominio = value;
}
}
#endregion
#region metodos
public override string ToString()
{
string cadena = "Dominio Camion: " + this.dominio + "Marca Camion: " + this.marca;
return cadena;
}
}
And this is the children of Equipos, called Gruas:
public class Grua : Camiones
{
#region atributos
protected int altura;
protected int peso;
protected double precioHora;
#endregion
#region constructores
public int Altura
{
get { return altura; }
set
{
altura = value;
}
}
public int Peso
{
get { return peso; }
set
{
peso = value;
}
}
public double PrecioHora
{
get { return precioHora; }
set
{
precioHora = value;
}
}
public override string ToString()
{
string cadena = base.ToString();
cadena += " - Peso Grua: " + peso + " Altura Grua: " + altura + " Precio Hora: " + precioHora + " Marca Grua: " + this.marca;
return cadena;
}
I've done a MessageBox to show if the class Camiones is using the ToString() fine, and it does work. But when i call it on my third class Gruas, the ToString() from Camiones only shows "Dominio Camion: -doesn't show the attribute dominio- Marca Camion: " -shows the attribute marca i set for Gruas, but it should show the attribute marca i set for Camiones!-
It seems like my third class is deleting the attributes i set for it's parent Class Camiones... i dont know why is this happening
This is how i set my properties on my main form:
Camiones nuevoCamion; nuevoCamion = new Camiones();
Grua nuevaGrua;
nuevaGrua = new Grua();
nuevoCamion.Marca = Convert.ToString(cbCamionMarca.SelectedItem);
nuevoCamion.Dominio = Convert.ToString(tCamionDominio.Text);
MessageBox.Show(nuevoCamion.Marca + " " + nuevoCamion.Dominio); //it shows everything it should show
MessageBox.Show(nuevoCamion.ToString()); //it also shows everything it should show
nuevaGrua.Marca = Convert.ToString(cbGruaTipo.SelectedItem);
nuevaGrua.Peso = Convert.ToInt32(mtPesoMaximo.Text.Trim());
nuevaGrua.Altura = Convert.ToInt32(mtAlturaIzaje.Text.Trim());
nuevaGrua.PrecioHora = Convert.ToDouble(mtPrecioGrua.Text);
parListaGruas.Add(nuevaGrua);
MessageBox.Show(parListaGruas[0].ToString()); //only shows Dominio Camion: with no data, and Camion Marca: with marca from Gruas. And it shows the rest of the Gruas.ToString() ok- This is where im having issues
MessageBox.Show(nuevoCamion.ToString()); //i called my nuevoCamion again to check if the data was still there, and its still there.