0

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.
D Stanley
  • 149,601
  • 11
  • 178
  • 240
le0nicolas
  • 83
  • 10
  • Looks fine at fist glance - can you show how you're setting the properties? – D Stanley Jun 29 '20 at 15:19
  • The code looks fine. It's *not* clear what the output is though. By putting everything in a single line it's hard to understand what the actual and desired output is, what's the problem, or what was expected. There's no code that *sets* anything, so it could easily be that the properties weren't set properly, or they were overwritten – Panagiotis Kanavos Jun 29 '20 at 15:27
  • @DStanley yes, i just edited it. I also added the MessageBox.Show with what my screen shows – le0nicolas Jun 29 '20 at 15:28
  • 1
    What is `parListaGruas`? Where is `nuevoCamion` initialized, what does it contain?We can't use this code to reproduce the issue – Panagiotis Kanavos Jun 29 '20 at 15:28
  • @PanagiotisKanavos sorry, i edited it wrong. Let me fix it – le0nicolas Jun 29 '20 at 15:33
  • 1
    `nuevoCamion` doesn't appears to be related to your `nuevaGrua`. `nuevaGrua.Dominio` is never set. Creating a new `Camiones` doesn't affect a new `Grua` object. – Jimi Jun 29 '20 at 15:33
  • Btw, all those fields (if really needed) should most probably be read-only (unless `Altura` and `Peso` might change over time - if those mean `height` and `weight`, they probably won't :). `PrecioHora` should be of decimal type. – Jimi Jun 29 '20 at 15:39
  • The question as written requires us to look at lots of properties and fields that aren't really relevant. If the question is just about inheritance then it doesn't matter what the `ToString()` methods return, and most of the properties aren't relevant. It just makes the question a lot harder to read. Try writing the bare minimum amount of code needed to verify that inheritance works the way you expect it to and just test that. And if doesn't work, just post that in a question, and it will be much easier to follow. – Scott Hannen Jun 29 '20 at 16:35

0 Answers0