In your TaxbillURL method, if the first if condition fails, there is nothing which says what this method should should return. I am not sure what value this method is supposed to return in this case. Please return the appropriate return value.
If mpropertynumber IS equal to an empty string your if block will not execute, and that's where all your return statements are.
Mark all posts that give the desired result the answer. If you only mark the last that gave you clarification because you misread an earlier post others will be confused. Some of us are here to help others and our point to post ratio matters.
As pointed by the message if you pass something else than "First", "Second" or "Delinquent" there is nothing telling which value should be returned (I believe you should have a warning as well in VB ?)
How to fix that depends what you want. If you expect only those values it might be better to throw an exception to tell that the mpropertynumber is not valid rather to return an empty string (ie the link would missing and nobody would know about that programming
error). In this case, I would also likely use an enum so that the developper can better know this method only expect those 3 values (or maybe 4) values.
Member
48 Points
274 Posts
Not all code paths return a value - Converting from VB.NET to C#
Apr 13, 2016 07:31 PM|spook_man|LINK
Hello..
Getting the following error; Error: CS0161 '_Default.TaxbillURL(string)': not all code paths return a value
On the main page, I have a grid view which is being populated via an ObjectDataSource.
The TemplateField has the following code (it takes the mpropertynumber and creates it as a dynamically created URL):
In the code behind I have the following and am getting the error.
Any ideas? Thanks..
Participant
1914 Points
485 Posts
Re: Not all code paths return a value - Converting from VB.NET to C#
Apr 13, 2016 07:46 PM|Nataraj Gandhi Arunachalam|LINK
Hi Spook_man,
In your TaxbillURL method, if the first if condition fails, there is nothing which says what this method should should return. I am not sure what value this method is supposed to return in this case. Please return the appropriate return value.
protected string TaxbillURL(string mpropertynumber)
{
if ((mpropertynumber != ""))
{
if ((strTaxBillHalf == "First"))
{
string query = QueryStringModule.Encrypt("&TaxHalf=First&TaxYear=" + (Session["taxyear"].ToString()) + ("&parcel=" + mpropertynumber));
TaxbillURL("ViewTaxBillOnlineByParcel.aspx" + query);
return TaxbillURL();
}
else if ((strTaxBillHalf == "Second"))
{
string query = QueryStringModule.Encrypt("&TaxHalf=Second&TaxYear=" + (Session["taxyear"].ToString()) + ("&parcel=" + mpropertynumber));
TaxbillURL("ViewTaxBillOnlineByParcel.aspx" + query);
return TaxbillURL();
}
else if ((strTaxBillHalf == "Delinquent"))
{
string query = QueryStringModule.Encrypt("&TaxHalf=Delinquent&TaxYear=" + (Session["taxyear"].ToString()) + ("&parcel=" + mpropertynumber));
TaxbillURL("ViewTaxBillOnlineByParcel.aspx" + query);
return TaxbillURL();
}
}
return "";
} // TaxbillURL END
Nataraj
----------------------
Please mark as answer if it helps!
Contributor
7048 Points
2189 Posts
Re: Not all code paths return a value - Converting from VB.NET to C#
Apr 13, 2016 07:47 PM|ryanbesko|LINK
If mpropertynumber IS equal to an empty string your if block will not execute, and that's where all your return statements are.
All-Star
17652 Points
3510 Posts
Re: Not all code paths return a value - Converting from VB.NET to C#
Apr 15, 2016 06:08 AM|Chris Zhao|LINK
Hi spook_man,
Methods with a non-void return type are required to use the return keyword to return a value.
https://msdn.microsoft.com/en-us/library/87cz4k9t(v=vs.140).aspx
Best Regards,
Chris Zhao
All-Star
48570 Points
18082 Posts
Re: Not all code paths return a value - Converting from VB.NET to C#
Apr 15, 2016 03:46 PM|PatriceSc|LINK
Hi,
As pointed by the message if you pass something else than "First", "Second" or "Delinquent" there is nothing telling which value should be returned (I believe you should have a warning as well in VB ?)
How to fix that depends what you want. If you expect only those values it might be better to throw an exception to tell that the mpropertynumber is not valid rather to return an empty string (ie the link would missing and nobody would know about that programming error). In this case, I would also likely use an enum so that the developper can better know this method only expect those 3 values (or maybe 4) values.