C# - 3 classes, Customer, MAIN and Validator. Need to validate a string in customer from a function present in validator class

Viewed 40

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!

2 Answers

In your case I'd make the Validator class static and you can simply call it in your customer class using Validator.HasCapitalLetter(customerName).

If you can't make it static then you can use the dependency injection pattern to pass an instance of the Validator class to your Customer class. However this seems like overkill for this situation where a static method would do just fine.

In general - to use it you need to create an INSTANCE of Validator class. So:

Validator val = new Validator();
bool result = val.HasCapitalLetter(<something>);

or change Validator to static class...

From the code I suppose it is pointless to introduce anything more here. Additionally you have a bug in the type of the argument for the constructor:

public Customer(int customerName)

Customer name is probably a string not int....

Related