|
-
November 26th, 2009, 05:20 PM
#1
Unreachable code detected
I can't work out what i've done wrong here, why is it telling me there is unreachable code on the "if" in this:
Code:
//Check email address entered is valid
public class RegexUtilities
{
public static bool IsValidEmail(string sEmailRecipient)
{
// Return true if sEmailRecipient is in valid e-mail format.
return Regex.IsMatch(sEmailRecipient,
@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
if (RegexUtilities.IsValidEmail(sEmailRecipient))
MessageBox.Show("Valid = " + sEmailRecipient);
else
MessageBox.Show("Inalid = " + sEmailRecipient);
}
}
Any ideas?
Thank you.
Last edited by saldous; November 26th, 2009 at 07:11 PM.
-
November 26th, 2009, 09:28 PM
#2
Re: Validating an email address error
Well, in your first code within IsValidEmail(), it always return the boolean result from Regex.IsMatch().
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
November 27th, 2009, 02:49 AM
#3
Re: Validating an email address error
Maybe you've wish to write it this way:
Code:
public static bool IsValidEmail(string sEmailRecipient)
{
// Return true if sEmailRecipient is in valid e-mail format.
bool isMatch = Regex.IsMatch(sEmailRecipient,
@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
if (isMatch)
MessageBox.Show("Valid = " + sEmailRecipient);
else
MessageBox.Show("Inalid = " + sEmailRecipient);
}
}
Note that you had recursion in your code, which will lead to StackOverflowException.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
November 27th, 2009, 04:55 AM
#4
Re: Validating an email address error
Thanks for your help all, but I'm still struggling.
with the code above (from boudino) I get the error:
"not all code paths return a value"
I'm starting to hate c# already, I've been playing with this same bit of code all night now
-
November 27th, 2009, 08:07 AM
#5
Re: Validating an email address error
It's not C#. It is basic programming. All functions must return a value to the calling procedure. This would happen in any language.
I am sure in re-arranging your code, buodino just missed adding in one line. Add the return statement after you if statement like below.
Code:
public static bool IsValidEmail(string sEmailRecipient)
{
// Return true if sEmailRecipient is in valid e-mail format.
bool isMatch = Regex.IsMatch(sEmailRecipient,
@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");
if (isMatch)
MessageBox.Show("Valid = " + sEmailRecipient);
else
MessageBox.Show("Inalid = " + sEmailRecipient);
return isMatch;
}
Or better yet, consolidate the code something like.
Code:
public static bool IsValidEmail(string sEmailRecipient)
{
// Return true if sEmailRecipient is in valid e-mail format.
if (Regex.IsMatch(sEmailRecipient,
@"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"))
return true;
else
return false;
}
I say that because you probably don't really want to show a messagebox in that routine, you probably want to just show it if it is invalid.
Last edited by sotoasty; November 27th, 2009 at 08:11 AM.
-
November 27th, 2009, 07:34 PM
#6
Re: Validating an email address error
How about?
Code:
public static bool IsValidEmail(string sEmailRecipient)
{
// Return true if sEmailRecipient is in valid e-mail format.
return Regex.IsMatch(sEmailRecipient, Constants.RegExExp.Email );
}
-
November 30th, 2009, 02:11 AM
#7
Re: Validating an email address error
Less code is better code
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
December 1st, 2009, 04:56 PM
#8
Re: Validating an email address error
Thanks all, think I've got it sorted now. you've all been very helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|