简体   繁体   中英

Method must have return type

I keep getting this error when I try to build my solution. I've tried public string and just public but can't get it to work. Any ideas? Thanks!

[WebMethod]
public static LoadCitiesByState(string state)
{
        DataTable dt = SharedDataAccess.GetCities(state);
        List<string> cities = new List<string>();

        foreach (DataRow row in dt.Rows)
        {
            cities.Add(row[0].ToString());
        }
        return cities;

   }

EDIT

Now I get the error: "Not all code paths return a value"

EDIT II

Sorry forgot to paste the try & catch I had in there. I removed them and it worked. If anyone wants to show how to get it to work with a try & catch, feel free though.

You need to specify the type of the variable you're returning:

public static List<string> LoadCitiesByState(string state)

edit for try/catch:

You need to make sure that something gets returned regardless of whether there is an exception thrown or not. So declare the return variable beforehand, and return it after the try/catch block:

public static List<string> LoadCitiesByState(string state)
{
    List<string> cities = new List<string>();
    try {
        DataTable dt = SharedDataAccess.GetCities(state);
    } catch // specify exceptions here
    {
        //exception handling
    }
    foreach (DataRow row in dt.Rows)
    {
        cities.Add(row[0].ToString());
    }
    return cities;
}

In general it's always a good idea to minimize the number of lines you have inside the try block.

您要返回的城市是List<string>因此应该是您的返回类型。

public static List<string> LoadCitiesByState(string state)

the problem with your method it's that you need to define which type of data are you going to return

[WebMethod]
public static List<string> LoadCitiesByState(string state)
{
    DataTable dt = SharedDataAccess.GetCities(state);
    List<string> cities = new List<string>();

    foreach (DataRow row in dt.Rows)
    {
        cities.Add(row[0].ToString());
    }
    return cities;

}

And that's all

UPDATE

If you want to add the try and catch you could do it like this

[WebMethod]
public static List<string> LoadCitiesByState(string state)
{

    List<string> cities = new List<string>();
    try
    {
        DataTable dt = SharedDataAccess.GetCities(state);
        foreach (DataRow row in dt.Rows)
        {
            cities.Add(row[0].ToString());
        }
     }
     catch(Exception e)
     {
          //logs your exception 
     }

    return cities;

}

You need to specify return type, try this

[WebMethod]
public static List<string> LoadCitiesByState(string state)
{
        DataTable dt = SharedDataAccess.GetCities(state);
        List<string> cities = new List<string>();

        foreach (DataRow row in dt.Rows)
        {
            cities.Add(row[0].ToString());
        }
        return cities;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM