在方法中返回时出错

时间:2015-04-05 22:10:48

标签: c# asp.net

在我的selectUser类中,我有:

 public checkEmail(string email)
    {
           peopleTableAdapters.PeoplesTableAdapter p = new peopleTableAdapters.PeoplesTableAdapter();
           return p.checkEmail(email);

    }

在客户端代码中我有:

protected void Button1_Click(object sender, EventArgs e)
{

 selectUser s = new selectUser();
 try
   {
      s.checkEmail(email.Text);       
   }
 catch(Exception e)
   {

   }
}

我收到了这个错误" return p.checkEmail(email);"错误是:

"错误307因为' authentication.checkEmail(string)'返回void,返回关键字后面不能跟一个对象表达式"

有谁知道我做错了什么?

我不想返回任何数据,因为它不需要我只想返回一些东西,所以它在try语句中成功。

2 个答案:

答案 0 :(得分:1)

这没有返回类型:

public checkEmail(string email)

我甚至对编译感到惊讶,但是当没有指定时,编译器可能会假定void?我不知道它做到了。但无论哪种方式,都没有返回类型。所以这不会起作用:

return p.checkEmail(email);

如果该方法没有返回类型,则它无法返回任何内容。您也没有对返回的值做任何事情:

s.checkEmail(email.Text);

所以你根本不需要退货。

此外,错误还表明p.checkEmail(email) 本身不会返回任何内容。因此,尝试返回本身void返回类型的内容只会使问题复杂化。

由于此代码中没有任何内容返回任何值,因此您可以通过根本不尝试返回任何内容来解决此问题:

public void checkEmail(string email)
{
    peopleTableAdapters.PeoplesTableAdapter p = new peopleTableAdapters.PeoplesTableAdapter();
    p.checkEmail(email);
}

答案 1 :(得分:0)

要exp [和大卫的答案,如果你不想让checkEmail方法返回一个值,那么你需要在链中的任何一点上为无效的电子邮件抛出一个异常,你确定它是是或否无效。

这可以使用throw new ArgumentException()将ArgumentException替换为您认为合适的任何异常类型来完成。