Last post Dec 18, 2018 08:15 AM by DA924
Dec 18, 2018 02:37 AM|wavemaster|LINK
The "return results'" in yellow makes the error message go away, but I am not sure this is the right solution.
public async Task<List<int>> GetChannelsInAlarm(string last15)
var results = new List<int>();
using (var cmd = new SqlCommand(@"
SELECT channel, channelalarm
GROUP BY channel HAVING [date] = MAX(date) AND last15sim = @0", connection))
cmd.Parameters.Add(new SqlParameter("@0", last15));
using (var reader = await cmd.ExecuteReaderAsync())
if (!reader.HasRows) return null;
while (await reader.ReadAsync())
long num = (long)reader["channel"];
if ((long)reader["channelalarm"] == 1)
results.Add((int)num - 1);
Dec 18, 2018 07:36 AM|Sherry Chen|LINK
Hi wavemaster ,
The return type expected by GetChannelsInAlarm method is a List<int>, so there must be a return Statement unless the method return type is viod .
Your solution is ok .
Best Regards ,
Dec 18, 2018 07:45 AM|PatriceSc|LINK
Correct. If omitted the list is filled but not returned and so the compiler complains because the method can run without telling which values is returned.
Mnor but a common approach when returning list is to return an empty list (rather than null) when your have no data.
Dec 18, 2018 08:15 AM|DA924|LINK
If you are using a method the returns a value, then the last statment in the method should
return the value.
If you had an If statement that did a return value, then it would leave the method at that point, kind of like short-circuiting out of the method, instead of falling all the way down to the bottom of the method and doing a return value
as an alternate path. If the If statement decision was not met to execute the
return taking an alternate path and that path had no return value, a possible leave out of the method, you will get the compile error messages 'That not all paths return a value'.
The compiler doesn't know that the if statment will always be met and the
return value executed. The compiler sees all paths and determines that not all paths the code can take. will result in a
return value being executed.
So the common practice is to have a return value at the bottom of a method that is going to
return a value, and it will cover all paths, becuase the code execution will fall to the bottom of the method if no path is taken that had a
return value, the safety net.