CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Posts
    34

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    34

    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

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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 );
    }

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  8. #8
    Join Date
    Nov 2009
    Posts
    34

    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
  •  





Click Here to Expand Forum to Full Width

Featured