简体   繁体   English

方法必须具有返回类型

[英]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. 我试过public string ,只是public但无法正常工作。 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. 对不起,忘记粘贴我在那儿的try&catch了。 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: 因此,事先声明返回变量,然后在try / catch块之后返回它:

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. 通常,最好将try块中的行数减到最少。

您要返回的城市是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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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