Hello, according to the official documentation I should remove the word return to solve the problem, but it is absurd, because if I want to show it, how will I remove the return? How do I solve it? Thank you.
namespace DESAFIOPOO1
{
class Program
{
static void Main(string[] args)
{
Telefono t1 = new Telefono();
t1.setModelo( "Motorola");
string mod = t1.getModelo();
Console.WriteLine("el modelo es" + " mod");
Console.ReadKey();
}
}
}
private string Modelo;
//private string Marca;
//private int NumeroTelefonico;
//private int CodigoOperador;
public string setModelo(string m)
{
Modelo = m;
}
public string getModelo()
{
return Modelo;
}
}
}
C#:private string Modelo; //private string Marca; //private int NumeroTelefonico; //private int CodigoOperador; public string setModelo(string m) { Modelo = m; } public string getModelo() { return Modelo; } } }
public string Modelo {get; set;}
You are right now it worked... I thought that the "gettear" method was where the return should goYour method signature on line 6 says you will return a string, so you need to return a string.
tHa ha ha you're right, but the instruction for the exercise says that I should do it like this.You code should simply be:
C#:public string Modelo {get; set;}
We do not write get/set methods in C#, we use properties. Why write 20 lines when you can write 1?
Please leave your Java at the door
But why then does the official c# error documentation say that I should comment out the return line? Cuadno for it to work just needs to be uncommentedYour method signature on line 6 says you will return a string, so you need to return a string.
The official documentation is showing how to cause the error, and how to modify the problem code to fix the errorBut why then does the official c# error documentation say that I should comment out the return line? Cuadno for it to work just needs to be uncommented
else
is performed, no return
keyword is encountered. C# is not an AI and it does not look at the condition of the if
and think "oh, the i variable is 5 and the if clause demands it be less than 10, and nothing changes the i variable so the else will never be used, so the code is ok" - it just looks at the code and sees "there is an if that does have a return and an else that does not, so the promise is broken and that method might not return what it promised. Throw an error"If i is greater than 10, it enters the else and if the return is commented, the return of the else will not be executed, only the one of the if has the possibility of being executed, because it is uncommentedIt says uncomment, then bothif
andelse
will have a return, so all code paths will return a value.
shouldn't it be the other way around? If the code promises to return an integer, shouldn't C# and your compiler just check that it returns an integer?The official documentation is showing how to cause the error, and how to modify the problem code to fix the error
The code says "int Main" which means "I faithfully promise this method will always return an integer". C# checks you are keeping your promise; it looks at the code and tries to find a way out of the method that does not return an integer. It finds one, because in the case that theelse
is performed, noreturn
keyword is encountered. C# is not an AI and it does not look at the condition of theif
and think "oh, the i variable is 5 and the if clause demands it be less than 10, and nothing changes the i variable so the else will never be used, so the code is ok" - it just looks at the code and sees "there is an if that does have a return and an else that does not, so the promise is broken and that method might not return what it promised. Throw an error"
public string SetModelo(string m)
{
_modelo = m;
}
public string SetModelo(string m)
{
int i = 5
if (i < 10)
{
_modelo = m;
return _modelo;
}
}
i
will always be less than 10.public string SetModelo(string m)
{
const int i = 5;
if (i < 10)
{
_modelo = m;
return _modelo;
}
}
const
values in the latest iterations of the compiler.The error is that some paths through the code do not return a value. This is not allowedshouldn't it be the other way around? If the code promises to return an integer, shouldn't C# and your compiler just check that it returns an integer?