Error : Not all code paths return a value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fretIT
    New Member
    • Dec 2007
    • 1

    #1

    Error : Not all code 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;
            }
        }
  • chroot
    New Member
    • Nov 2007
    • 13

    #2
    Your function has a return value (string). The function must return a value in any case. Now if the program doesn't enter your if, it will reach the end of the function, where there's no return value => error.

    The compiler is smart enough for this, though:

    Code:
    int abs(n) {
    if (n > 0)
      return n;
    else
      return -n;
    }
    There's something returned in any case, so the compiler won't complain.

    Comment

    Working...