Reputation: 585
public int Queue()
{
using (Entities server = new Entities())
{
var ServerId1 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 1
select serverID.ServerId).Count();
var ServerId2 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 2
select serverID.ServerId).Count();
var ServerId3 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 3
select serverID.ServerId).Count();
if (ServerId1 == 0 && ServerId2 == 0 && ServerId3 == 0)
{
return ServerId1;//Convert.ToInt32(ServerId1);
}
else if (ServerId1 == 1 && ServerId2 == 0 && ServerId3 == 0)
{
return ServerId2;
}
else if (ServerId1 == 1 && ServerId2 == 1 && ServerId3 == 0)
{
return ServerId3;
}
else if (ServerId1 > ServerId2 && ServerId1 > ServerId3)
{
if (ServerId2 > ServerId3)
{
return ServerId3;
}
else
{
return ServerId2;
}
}
else if (ServerId2 > ServerId3 && ServerId2 > ServerId1)
{
if (ServerId1 > ServerId3)
{
return ServerId3;
}
else
{
return ServerId1;
}
}
else if (ServerId3 > ServerId1 && ServerId3 > ServerId2)
{
if (ServerId1 > ServerId2)
{
return ServerId2;
}
else
{
return ServerId1;
}
}
else if (ServerId1 == ServerId2 && ServerId2 == ServerId3 && ServerId1 == ServerId3)
{
return ServerId1;
}
else if (ServerId1 == ServerId2 && ServerId1 > ServerId3 && ServerId2 > ServerId3)
{
return ServerId3;
}
else if (ServerId2 == ServerId3 && ServerId2 > ServerId1 && ServerId3 > ServerId1)
{
return ServerId1;
}
else if (ServerId1 == ServerId3 && ServerId1 > ServerId2 && ServerId1 > ServerId3)
{
return ServerId1;
}
}
}
I am getting the error(not all code paths return a value) in the method. This method returns an int value as i have defined. But don't know why this error happens even if i have returned an int value. Could any one help me out?
Upvotes: 0
Views: 125
Reputation: 4965
If not at least one of the else if
s or if
are met, then there is no "default" return.
Add a return
at the very end, or change the last else if
to just an else
.
Upvotes: 0
Reputation: 203821
There are a lot of other answers here that are addressing your specific error message, but there are more underlying issues with your method that have been overlooked. You have a giant series of if/else if statements that are enormously long and complex just to return the minimum of 3 integers. There are much easier ways of doing that. The method with the least refactoring would be to add the following immediately after your queries:
return new[] { ServerId1, ServerId2, ServerId3 }.Min();
However, you could refactor the query itself so that you only perform one query, rather than three, and don't hard code in the three different server IDs. This would allow you to replace everything inside of the using
with:
return (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted"
group serverID by serverID.ServerId into servers
select servers.Count())
.Min();
Upvotes: 3
Reputation: 810
public int Queue()
{
using (Entities server = new Entities())
{
int retVal=0;//initialize it your value
var ServerId1 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 1
select serverID.ServerId).Count();
var ServerId2 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 2
select serverID.ServerId).Count();
var ServerId3 = (from serverID in server.AppPM_Patches
where serverID.PatchStatus == "NotStarted" && serverID.ServerId == 3
select serverID.ServerId).Count();
if (ServerId1 == 0 && ServerId2 == 0 && ServerId3 == 0)
{
retVal=ServerId1;//Convert.ToInt32(ServerId1);
}
else if (ServerId1 == 1 && ServerId2 == 0 && ServerId3 == 0)
{
retVal=ServerId2;
}
else if (ServerId1 == 1 && ServerId2 == 1 && ServerId3 == 0)
{
retVal=ServerId3;
}
else if (ServerId1 > ServerId2 && ServerId1 > ServerId3)
{
if (ServerId2 > ServerId3)
{
retVal= ServerId3;
}
else
{
retVal=ServerId2;
}
}
else if (ServerId2 > ServerId3 && ServerId2 > ServerId1)
{
if (ServerId1 > ServerId3)
{
retVal=ServerId3;
}
else
{
retVal=ServerId1;
}
}
else if (ServerId3 > ServerId1 && ServerId3 > ServerId2)
{
if (ServerId1 > ServerId2)
{
retVal=ServerId2;
}
else
{
retVal=ServerId1;
}
}
else if (ServerId1 == ServerId2 && ServerId2 == ServerId3 && ServerId1 == ServerId3)
{
retVal=ServerId1;
}
else if (ServerId1 == ServerId2 && ServerId1 > ServerId3 && ServerId2 > ServerId3)
{
retVal=ServerId3;
}
else if (ServerId2 == ServerId3 && ServerId2 > ServerId1 && ServerId3 > ServerId1)
{
retVal=ServerId1;
}
else if (ServerId1 == ServerId3 && ServerId1 > ServerId2 && ServerId1 > ServerId3)
{
retVal=ServerId1;
}
return retVal;
}
}
Upvotes: 1
Reputation: 218828
You have an if
and a bunch of else if
statements, but you don't have an else
statement. As far as the compiler is concerned, it's perfectly conceivable that none of the if
conditions will be satisfied. In that case, the method has no return value.
You can return a value in an else
statement, or after the if
construct in general. Alternatively, if the code should indeed never exit the if
statement without satisfying any of the conditions then you can throw an exception at the end of the method to indicate that there was an error. An exception is an acceptable exit strategy for a method.
Upvotes: 5
Reputation: 16007
If you reach the end of the function, you're not returning anything yet.
Upvotes: 0
Reputation: 19888
You dont have an else case so you havent covered all cases. You might have logically covered all cases but syntactically you didn't. So maybe you might want to return the first server id and just add
else
{
return ServerId1;
}
Upvotes: 1