Circle area application: method shows that not all code paths return value

Asked

Viewed 102 times

-1

I have a code that performs the calculation of the area of the circle on three insatiated objects in AppCírculo, only that the method getArea() that should return the area of the circle with the calculation, only that Visual Studio says that not all paths of the code return value, message coming from error CS0116 that I do not understand.

using static System.Console;

namespace Aplicacao1
{
    public class Circulo
    {
        private double raio;
        private double area;

        public double getRaio()
        {
            return this.raio;
        }

        public void setRaio(double raio)
        {
            this.raio = raio;
        }

        public double getArea()
        {
            Circulo circulo1 = this;
            Circulo circulo2 = this;
            Circulo circulo3 = this;
            circulo1.area = 3.14159 * (raio * raio);
            circulo2.area = 3.14159 * (raio * raio);
            circulo3.area = 3.14159 * (raio * raio);
        }

        public string toString()
        {
            return raio + " " + area + " ";
        }
    }
}
using static System.Console;

namespace Aplicação1
{
    public class AppCirculo
    {
        static int Main(string[] args)
        {
            Circulo circulo1 = new Circulo();
            Circulo circulo2 = new Circulo();
            Circulo circulo3 = new Circulo();

            Write("Digite o raio do círculo 1: ");
            circulo1.setRaio(double.Parse(ReadLine())); 

            Write("Digite o raio do círculo 2: ");
            circulo2.setRaio(double.Parse(ReadLine()));

            Write("Digite o raio do círculo 3: ");
            circulo3.setRaio(double.Parse(ReadLine()));

            WriteLine(circulo1.ToString());
            WriteLine(circulo2.ToString());
            WriteLine(circulo3.ToString());
            ReadKey();
            return 0;
        }
    }
}
  • public double getArea() is a return method double but, missed you put this in your code return with the value?

  • would be return this.area?

  • So Carlos does not know what the return is and how he wants to finalize this method ... only that he must have one return with a certain type value double

  • @Virgilionovic wanted to return a value double

  • I do not know why you are making 3 circle variables pointing to the object itself and then doing the same operation ...! don’t just return ??? that operation.

1 answer

2


When you use a type as a return in signature of the method you have to return an object of that type in any situation that happens in the execution of the method. In general this error occurs when you return in one path but not in another, i.e., you have a conditional part that makes the return and another condition that does not. But in this case there is nothing conditional, so it should return something in a simple way, but in this case there is no return.

Or else the solution is to change the type of the method and say that it returns void, in this case you do not need to return anything else, but you cannot use this method where some return is expected.

By the method I would try to guess what he should do, so by the name I would say you have to leave the guy double in the method getArea() and have a return. But then I would have to decide what to return, and complicated because this method does a lot of things but it doesn’t take area of the circle, so I don’t even know what to return. You realize that even if you fix this error your code will still be confused?

I’m going to guess that the method would be like this, but probably this is a mistake (it doesn’t make sense to have the method like this). The whole class is confused.

public double GetArea() => 3.14159 * raio * raio;

Note that I changed the name to stay the way they usually do me C#. And I took a lot of things that I thought didn’t make sense. And I used a syntax where the return is implicit, so you don’t need to use the return, but he’s returning anyway, he returns just the calculation contained there, it would be the same as doing:

public double GetArea() {
    return 3.14159 * raio * raio;
}

And if you think about C# the right thing is to use a property and not a method to do this, like this:

public double Area => 3.14159 * raio * raio;

And the lightning should be like this:

public double Raio { get; set; }

And you don’t even need the field raio which is created automatically. So you don’t need the methods getter/Setter which you created is not idiomatic in C#.

I don’t see why the countryside area should exist, if you think you should then justify.

also read about builders, because this is also wrong in the class, in fact I consider the use of Setter even in the property is mistaken for this example. I believe you are learning to do wrong and I tried to give you several tips on what can improve. Search here on the site more about everything you are trying to do, you will learn a lot, don’t always continue to learn the right way to do things because this will consolidate and always continue to err,

Normally use the ToString() the way you’re doing is considered abuse, in this case nothing exaggerated, but can be considered wrong.

There are other problems in the code that do not prevent the functioning or that are not noticeable if you do not enter wrong data, but I will not try to solve them all here.

To give you a parameter of what would be good code for this. Analyze carefully to see everything you can best in what you are doing:

using static System.Console;

public class Circulo {
    public Circulo(double raio) => Raio = raio;
    public double Raio { get; }
    public double Area => 3.14159 * Raio * Raio;
    public string Imprima() => Raio + " " + Area + " ";
}

public class AppCirculo {
    static int Main() {
        Write("Digite o raio do círculo 1: ");
        if (!double.TryParse(ReadLine(), out var valor)) return 1;
        var circulo1 = new Circulo(valor);
        Write("Digite o raio do círculo 2: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo2 = new Circulo(valor);
        Write("Digite o raio do círculo 3: ");
        if (!double.TryParse(ReadLine(), out valor)) return 1;
        var circulo3 = new Circulo(valor);
        WriteLine(circulo1.Imprima());
        WriteLine(circulo2.Imprima());
        WriteLine(circulo3.Imprima());
        return 0;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • How to get rid of IDE1006 that says "Nomenclature violation rule"?

  • I imagine it is in my answer, if not you should open another question. Each problem should be in a different question.

  • The use of properties is in a next question, where I have to make an application of the area of the circle and use properties for the same question, ie the same question made in two different forms.

  • ToString was my mistake.

  • The form without C# properties is almost always wrong.

  • I used public double GetArea() {
 return 3.14159 * raio * raio;
} I used it like this and when it was compiling appearedAplicacao1.Circulo, Aplicacao1.Circulo, Aplicacao1.Circulo

  • I edited the answer and put more tips. Your code has many problems, I think I’ll even rewrite it to see how well it looks.

  • You use properties in code?

Show 3 more comments

Browser other questions tagged

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