Why does it return zero value?

1

in my programming class in form I have to show the area in a message, only that they want the data of height and width to get it from the inheritance of the class and in the way that the I'm doing it always returns zero in the value of the area

DEMONSTRATION OF THE CODE

/// RECTANGLE CLASS

class Rectangulo : PresentationObject
{
    public int resultadoArea;
    public int ResultadoArea
    {
        get
        {
            return this.resultadoArea;
        }

        set
        {
            this.resultadoArea = value;
        }

    }

    public Rectangulo()
    {
        this.ResultadoArea = width * height;         
    }   
}

//CLASE PRESENTATION OBJECT
class PresentationObject
    {
        public int width;
        public int height;    

        public int Width
        {
            get
            {                 
                 return this.width;                 
            }

            set
            {
                this.width = value;                    
            }


        }

        public int Height
        {
            get
            {
                return this.height;
            }

            set
            {    
                this.height = value; 
            }    
        }


        public PresentationObject()
        {

        }

        public PresentationObject(int width,int height)
        {
            this.Width = width;
            this.Height = height;                
        }
    }

///BOTON DONDE MUESTRA EL MENSAJE

    private void btbCalcular_Click(object sender, EventArgs e)
        {
            Rectangulo rect = new Rectangulo();
            MessageBox.Show("El area es: " + rect.resultadoArea , "Calcular Area", MessageBoxButtons.OK);
        }
    
asked by JT25 04.10.2018 в 08:54
source

2 answers

5

You could make several changes but by adjusting to your code you have to take into account the following things:

  • As you have it, when you instantiate the class Rectangulo with Rectangulo rect = new Rectangulo(); the values of height and width are zero since you have not initialized them anywhere, hence the result of this.ResultadoArea = width * height; of the constructor of the class Rectangulo is zero

  • The constructors of PresentationObject right now do not make much sense since you're not using them

    public PresentationObject(int width, int height)
    {
        this.Width = width;
        this.Height = height;
    }
    

If you want to use them you should call them in the constructor of the class Rectangulo through base :

    public Rectangulo(int wid, int hei):base(wid, hei)
    {
    }

And when instances the class passes them as parameters like this:

Rectangulo rect = new Rectangulo(23,54);

In this way the parameters "arrive" to the constructor of PresentationObject and assign the values to Width and Height so that they can be used by the class Rectangulo inheriting from PresentationObject

  • Finally, to calculate the area, it is best to create a CalcularArea() method in your class Rectangulo that assigns the value of width * height to ResultadoArea

.

public void CalcularArea(){
    ResultadoArea = width * height;
    }

And then call him like this:

Rectangulo rect = new Rectangulo(23,54);
rect.CalcularArea();
MessageBox.Show("El area es: " + rect.resultadoArea , "Calcular Area", MessageBoxButtons.OK);

I do not know if I've finished explaining well. Lots of encouragement with those lessons! :)

    
answered by 04.10.2018 в 09:57
5

You have a problem of concept, something normal if you are starting in OOP.

Your class Rectangulo inherits PresentationObject , thereby inheriting its properties.

When you create an object of type Rectangulo , what you do is create an instance of that type, and that instance has several properties: own ( ResultadoArea ) and inherited ( Width and Height ) . But for that particular instance, those properties have no value since you have not given them.

Let's go to your code. On the one hand, in the class PresentationObject the variables width and height must be private. Access to these variables must be done using the properties ( Width and Height ):

class PresentationObject
{
    private int width;
    private int height;    
    ...

On the other hand, the constructor of PresentationObject that receives the parameters width and height is not necessary (although it does not do any harm to have it).

Finally, what you must do is change the constructor of the class Rectangulo so that you can initialize the properties necessary for the calculation:

public Rectangulo(int width, int height)
{
    // inicializamos las propiedades Width y Height que no forman parte de esta clase, 
    // sino que son heredadas de PresentationObject
    this.Width = width; 
    this.Height = height;
    // Ahora ya podemos calcular el area
    this.ResultadoArea = this.Width * this.Height;
}

The way to use it would be the following:

Rectangulo rect = new Rectangulo(30,50);
MessageBox.Show("El area es: " + rect.resultadoArea, "Calcular Area", MessageBoxButtons.OK);

I hope that with this explanation you have been clearer the concepts that are at stake in this problem. If not, do not hesitate to ask.

    
answered by 04.10.2018 в 10:03