Untreated POO code exception (Get and Set) from C#

Asked

Viewed 138 times

5

The code in question is used to calculate the circle area value using keywords get and set.

After I occurred CS1729 error a constructor was created to fix it

private double valor;

    public CirculoApp(double valor)
    {
        this.valor = valor;
    }

When I run the code the following output is shown:

Digite o raio do círculo 1: 5
Digite o raio do círculo 2: 6
Digite o raio do círculo 3: 7

Process is terminated due to StackOverflowException.

Error appears on line 14:

public double Raio
    {
        get => Raio; X
        set => Raio = value;
    }
X = System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.'

Here is the code.

using static System.Console;

public class CirculoApp
{
    private double valor;

    public CirculoApp(double valor)
    {
        this.valor = valor;
    }

    public double Raio
    {
        get => Raio;
        set => Raio = value;
    }

    public double Area
    {
        get => Area;
        set => Area = 3.14159 * Raio * Raio;
    }

    public string toString() => Raio + " " + Area + " ";
}

public class PropCirculoApp
{
    static int Main(string[] args)
    {
        Write("Digite o raio do círculo 1: ");
        if (!double.TryParse(ReadLine(), out var valor)) return 1;
        var circulo1 = new CirculoApp(valor);
        Write("Digite o raio do círculo 2: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo2 = new CirculoApp(valor);
        Write("Digite o raio do círculo 3: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo3 = new CirculoApp(valor);
        WriteLine(circulo1.toString());
        WriteLine(circulo2.toString());
        WriteLine(circulo3.toString());
        ReadKey();
        return 0;
    }
}

1 answer

3


The problem is because in the fragment:

public double Raio
{
    get => Raio;
    set => Raio = value;
}

You are instructing the compiler that when using the advisory method get it returns itself generating an endless auto recursion that ends in a call stack overflow.

The same problem occurs here:

public double Area
{
    get => Area;
    set => Area = 3.14159 * Raio * Raio;
}

To fix this problem just tailor the call using the field valor, which you have already stated in the code, the purpose of which is to store the radius of the circle.

using static System.Console;

public class CirculoApp
{
    private double valor;
    

    public CirculoApp(double valor)
    {
        this.valor = valor;
    }

    public double Raio
    {
        // Use dado contido no campo valor como o raio.
        get => valor;
        set => valor = value;
    }

    public double Area
    {
        //Aqui não é necessário o set pois a finalidade da propriedade é retornar o resultado do cálculo de área.
        get => 3.14159 * Raio * Raio;
    }

    public string toString() => Raio + " " + Area + " ";
}

public class PropCirculoApp
{
    static int Main(string[] args)
    {
        Write("Digite o raio do círculo 1: ");
        if (!double.TryParse(ReadLine(), out var valor)) return 1;
        var circulo1 = new CirculoApp(valor);
        Write("Digite o raio do círculo 2: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo2 = new CirculoApp(valor);
        Write("Digite o raio do círculo 3: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo3 = new CirculoApp(valor);
        WriteLine(circulo1.toString());
        WriteLine(circulo2.toString());
        WriteLine(circulo3.toString());
        ReadKey();
        return 0;
    }
}

Resulting:

Type the radius of circle 1:3

Type the radius of circle 2:5

Type the radius of circle 3:7

3 28.27431

5 78.53975

7 153.93791

Repl.it

  • Now I understand, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.