0

In my selectUser class I have:

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

    }

In the client side code I have:

protected void Button1_Click(object sender, EventArgs e)
{

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

   }
}

I am getting this error with the "return p.checkEmail(email);" the error is:

"Error 307 Since 'authentication.checkEmail(string)' returns void, a return keyword must not be followed by an object expression"

Does anyone understand what I am doing wrong?

I dont want to return any data because its not needed I just want to return something so it succeeds in the try statement.

4
  • This code should fail with Method (.....) must have a return type Commented Apr 5, 2015 at 22:21
  • 1
    You should review C# Coding Conventions (msdn.microsoft.com/en-us/library/ff926074.aspx) as you naming of functions and classes is very counter-intuitive. Commented Apr 5, 2015 at 22:22
  • @KasparsOzols The selectUser is a class. They're both different parts of code in different classes Commented Apr 5, 2015 at 22:33
  • Exactly. And class names should be in Pascal case. Instance names should be in Camel case. Like in SelectUser selectUser = new SelectUser(). If you stick to that rule, you will soon find that it is much more easier to read such code. Commented Apr 5, 2015 at 22:35

2 Answers 2

1

This has no return type:

public checkEmail(string email)

I'm surprised this even compiles, but maybe the compiler assumes void when nothing is specified? I didn't know it did that. But either way, there's no return type. So this won't work:

return p.checkEmail(email);

If the method doesn't have a return type, then it can't return anything. You also don't do anything with the returned value:

s.checkEmail(email.Text);

So you don't need to return anything at all.

Furthermore, the error also suggests that p.checkEmail(email) itself doesn't return anything. So trying to return something which itself is a void return type is just compounding the problem.

Since nothing in this code returns any value, you can probably resolve this by simply not trying to return anything at all:

public void checkEmail(string email)
{
    peopleTableAdapters.PeoplesTableAdapter p = new peopleTableAdapters.PeoplesTableAdapter();
    p.checkEmail(email);
}
Sign up to request clarification or add additional context in comments.

3 Comments

But it now says "Method must have a return type"
@Samrikan: And it didn't say that before? At all? I still highly suspect that public checkEmail(string email) isn't valid syntax. I'd never heard of C# automatically inferring a void return type. You may have to specify it manually. It's likely that this error was also happening in your original code and you simply weren't paying attention to the error.
just add void between public and checkMail in function definition
0

To exp[and on David's answer, if you don't want either checkEmail method to return a value, then you will need to throw an exception for an invalid e-mail at whatever point in the chain you are determining that it is or isn't valid.

This can be done using throw new ArgumentException() replacing ArgumentException with whatever exception type you feel is appropriate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.