So I am writing a c# program where the client recieves a character from ther server, and runs a background worker depending on which character it reiceves. The problem though is the button states not all code paths return a value:
private object SL_Click(object sender, EventArgs e)
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect(RecieveIP.Text, 8001); // use the ipaddress as in the server program
MessageBox.Show("Connected");
Console.Write("Enter the string to be transmitted : ");
String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
MessageBox.Show("Transmitting.....");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i )
Console.Write(Convert.ToChar(bb[i]));
string toattk = k.ToString();
if (toattk == "g")
{
googlesearch.RunWorkerAsync();
}
else if (toattk == "y")
{
ysearch.RunWorkerAsync();
}
else if (toattk == "a")
{
aolsearch.RunWorkerAsync();
}
else if (toattk == "yo")
{
youtubesearch.RunWorkerAsync();
}
else if (toattk == "s")
{
ssearch.RunWorkerAsync();
}
tcpclnt.Close();
}
catch (Exception)
{
MessageBox.Show("Could not find server computer.");
}
}
It says SLCLick - not all code paths return a value. This seems odd as there is an action for every if then statement? Thanks very much.
CodePudding user response:
the method need to return object
private object SL_Click(object sender, EventArgs e){
.....
return whatObject
}
or change return type to void
private void SL_Click(object sender, EventArgs e){
.....
}
CodePudding user response:
This seems odd as there is an action for every if then statement?
This isn't what the compiler is complaining about. Indeed it's possible to write if
statements that do nothing:
if(someVariable == someValue){
} else {
}
That's perfectly legal C#; "not all code paths return a value" is not about your method here calling off into some other method at various opportunities
The message arises because you made a promise here:
private object SL_Click
^^^^^^
"I promise to return something"
and you haven't fulfilled it; a method that declares some type of object will be returned must, somewhere within its body code, have return ...something here...
.
The "something here" must be of a Type that is (convertible to) the Type you promised in the method header
You cannot:
public string MyMethod(){. //I promise to return a string
return 1; //not a string, not implicitly convertible to a string
}
Further, there must be no detectable route through the code, through all the ifs and elses etc, that means C# can get to the end of the method without hitting a return ...something here...
public string MyMethod(){ //I promise to return a string
if(DateTime.Today.DayOfWeek == DayOfWeek.Monday){
return "Hello";
}
}
This also gives "not all paths return a value" - you have to completely and explicitly tell C# what to do in every case; you can't just leave it to decide itself what to return if it's Tuesday
If you don't plan to return anything, declare the method void
:
public void MyMethod(){
if(DateTime.Today.DayOfWeek == DayOfWeek.Monday){
return; //I don't work Mondays
}
}
You can still quit early in a void
method, by doing a return
without a value after it. Also you can let C# reach the end without encountering any return
- C# knows what to do if it reaches the end of a void method without having encountered a return, but for anything non-void you have to be explicit about what value to return on every possible path through the code
Small additional, because it's bound to come up for you soon- how do you return more than one value? In C# you can only ever return one thing. If you want to return multiple values, you're free to make something that holds multiple values, make one of those things and put your multiple values inside it.
That one thing could be an array, a dedicated class, some built in convenient like a a tuple or anonymous type.. but all in remember you can only return one thing; if you have lots of values to return, put them inside one thing and return that
In your case I'd say it should be a void
method. Try also to avoid returning object
- use a more specific type if you can.
We'll get to Task-returning methods some other time :)
CodePudding user response:
I think this is an event handler, and if you look closely your method demands an object to be returned private object SL_Click(object sender, EventArgs e)
.
If you can, then change the return type of the method from object
to void
private object SL_Click(object sender, EventArgs e)
In case you can't change the method signature, then you can just simple return any object (provided you don't need to return anything) at the end of the try
and catch
or add a finally
block and return a dummy object.
finally
{
return "Event Handled";
}