Thursday, July 30, 2009

Error : Not all paths return a value?

Hello,





when I write web method using C# in Visual basic 2005,


I can't return the string value to the client request.


I got such kind of error Not all code paths return a value.


Don't know how to cope that problem.





My code is as follow.





Thanks in advance

















=================Code==============


using System;


using System.Web;


using System.Web.Services;


using System.Web.Services.Protocols;





[WebService(Namespace = "http://www.tp.edu.sg/")]


[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


public class CountryLanguage : System.Web.Services.WebService


{


public CountryLanguage()


{





//Uncomment the following line if using designed components


//InitializeComponent();


}





[WebMethod(Description = "Return official language of particular country.")]


public string OfficialLanguage(string country)


{


string l = "Singapore"; string r = "English";


//if(l.CompareTo(country)==0)


int res = String.Compare(country,l);


if (res == 0)


{


return r;


}


}

Error : Not all paths return a value?
If (res == 0) test fails, you don't specify a return value. Need to do something like:





if (res == 0)


return r;


else


return .... some value ...
Reply:The simplest thing you can do is insert this line before the closing mark "}" of your method:





return String.Empty;





or





return null;





Good luck!
Reply:hey You have to return some value when res!=0 also





if(res==0)


{


return r;


}


else


{


return somethingelse;


}
Reply:the function OfficialLaunguage is supposed to return a string.


It only returns a string if res == 0.


No comments:

Post a Comment