Reputation: 2615
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
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
Reputation: 218837
static
.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
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
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