I am using a property in 1st class Customer and I am passing values for the constructor Customer in the main class. But I want to use a third class as well in which there are different functions to validate certain conditions. for example:
Here I have Customer class in which there is CustomerName property and i want to validate it that it can only have Capital and small case letter along with spaces in them. I am using Program.cs class to make this class's instances and give value to be set from there. How do i use this third class 'Validator.cs' with HasCapitalLetter bool method to validate if it has capital letter or not. Code:
namespace CustomerBL
{
public class Customer
{
private string customerName;
public string CustomerName
{
get => customerName;
set
{
if (Regex.IsMatch(value, "^[a-zA-Z ]") && (value.Length >=5 &&
value.Length <= 20))
{
customerName = value;
}
else
Console.WriteLine("enter correct format for Customer Name");
}
}
public Customer(int customerName)
{
this.customerName = customerName;
}
}
}
//Main Class
internal class Program
{
static void Main(string[] args)
{
Category obj = new Category("Varun gupta");
obj.CategoryName = "1var322";
Console.WriteLine("{0}", obj.CategoryName);
}
}
//Third class 'Validator' with the validator method
namespace ValBL
{
public class Validator
{ //need to use this method to validate while passing the value to
constructor as parameter
public bool HasCapitalLetter(string value)
{
if(Regex.IsMatch(value,"^[a-zA-Z ]"))
{
return true;
}
else
{
return false;
}
}
}
}
Hope you got the question, help is much appreciated. Let me know if more clarification is needed. Thanks!