taji01
taji01

Reputation: 2615

Use regular expression from another method in C#

I separated my RegularExpression() method from my validate() method that has all the if statements. How can I use my regex codes if it's separated like this?

I'm fairly new to programming, and I'm still learning how to use methods.

 public void Validate()
    {

        RegularExpression();

      if (PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text) == false) 
            {
                MessageBox.Show("Invalid cellphone number");
            }

      if (Email_Regex.IsMatch(Email_txBox.Text) == false) 
            {
                MessageBox.Show("Invalid E-Mail");
            }
    }

 public RegularExpression(object PhoneNumber_Regex)
    {
       var PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");

       var Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");

    }

Upvotes: 2

Views: 193

Answers (4)

BugFinder
BugFinder

Reputation: 17858

In

public RegularExpression(object PhoneNumber_Regex)
{
   var PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");

   var Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");

}

You declare 2 variables - but scope means those variables dont exist outside that call, so, they arent available for use.

However, if as part of the class you declared

readonly Regex Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");

so you have a read only variable you could then use like you thought as part of any function within that class

  if (Email_Regex.IsMatch(Email_txBox.Text) == false) 
  {
      MessageBox.Show("Invalid cellphone number");
  }

Upvotes: 1

David
David

Reputation: 218837

  1. While it's not the only way to share things between methods, in this particular case it would make sense to use class-level members.
  2. Your regular expressions themselves are unlikely to change, and can probably be static.
  3. Initialize them in a constructor so it's automatic instead of having to manually call the initializer.

This all adds up to something more like this:

public class MyClass
{
    private static Regex PhoneNumber_Regex { get; set; }
    private static Regex Email_Regex { get; set; }

    static MyClass
    {
        PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
        Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");
    }

    public void Validate()
    {
        if (!PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text)) 
            MessageBox.Show("Invalid cellphone number");
        if (!Email_Regex.IsMatch(Email_txBox.Text)) 
            MessageBox.Show("Invalid E-Mail");
    }
}

Upvotes: 0

Yasser Shaikh
Yasser Shaikh

Reputation: 47784

Should do something on this line.

public static class MyValidator
{
  protected static PhoneNumberRegex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
  protected static EmailRegex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");  

  public static bool ValidatePhoneNumber(string strToMatch)
  {
    return PhoneNumberRegex.IsMatch(strToMatch);
  }

  public static bool ValidateEmail(string strToMatch)
  {
    return EmailRegex.IsMatch(strToMatch);
  }
}

and then use it like this

if (!MyValidator.ValidatePhoneNumber(PhonNumb_txBox.Text)) 
{
    MessageBox.Show("Invalid cellphone number");
}

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626794

Add a static class where you will declare your regular expressions:

public static class MyRegexps
{
    public static readonly Regex PhoneNumber_Regex = new Regex(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$");
    public static readonly Regex Email_Regex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$.");
}

Then use them in your caller method:

if (MyRegexps.PhoneNumber_Regex.IsMatch(PhonNumb_txBox.Text) == false) 
{
    MessageBox.Show("Invalid cellphone number");
}

Upvotes: 5

Related Questions