locked
Not all code paths return a value - Converting from VB.NET to C# RRS feed

  • Question

  • User-1134314964 posted

    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):

    </asp:TemplateField>
    	<asp:TemplateField HeaderText="Parcel Number" SortExpression="mpropertynumber" ItemStyle-HorizontalAlign="Center">
    	<ItemTemplate>
    		<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# TaxbillURL(Convert.ToString(Eval("mpropertynumber"))) %>' Text='<%# Eval("mpropertyNumber") %>' Target="_blank"></asp:HyperLink>
    	</ItemTemplate>
    	<ItemStyle Width="100px"></ItemStyle>
    </asp:TemplateField>

    In the code behind I have the following and am getting the error.

    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();
    			}
    		}
    	} // TaxbillURL END

    Any ideas? Thanks..

    Wednesday, April 13, 2016 7:31 PM

Answers

  • User-286291038 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 13, 2016 7:46 PM
  • User-2057865890 posted

    Hi spook_man,

    Methods with a non-void return type are required to use the return keyword to return a value.

    protected string TaxbillURL(string mpropertynumber)
    {
       if ((mpropertynumber != ""))
    {
    }
    return "";
    }

    https://msdn.microsoft.com/en-us/library/87cz4k9t(v=vs.140).aspx

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 6:08 AM
  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 3:46 PM

All replies

  • User-286291038 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 13, 2016 7:46 PM
  • User2053451246 posted

    If mpropertynumber IS equal to an empty string your if block will not execute, and that's where all your return statements are.

    Wednesday, April 13, 2016 7:47 PM
  • User-2057865890 posted

    Hi spook_man,

    Methods with a non-void return type are required to use the return keyword to return a value.

    protected string TaxbillURL(string mpropertynumber)
    {
       if ((mpropertynumber != ""))
    {
    }
    return "";
    }

    https://msdn.microsoft.com/en-us/library/87cz4k9t(v=vs.140).aspx

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 6:08 AM
  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 3:46 PM