Search code examples
c#asp.netazureglobal-methods

Global methods in C# ASP.Net


I´m using C# and ASP.NET for developing in Windows Azure.

I want to do a global Method to validate fields, my idea is to do a global Method like (for example in Site.Master)

static Regex dateRegex = new Regex("^\\d{2}/\\d{2}/\\d{4}$");
public boolean validate( string type, string stringToValdate)
{
  boolean valid = NO;
  if (type = "date")
  {
      Match m = fechaRegex.Match(stringToValdate);
      if(m.Success)
      {
            valid = true;
      }
  }

 return valid;
}

And then use it in another webform

using ????

Site.Master.validate("date",TextBox1.Text);

Solution

  • You can use Extension Method on Master Type

    public static class Extension
    { 
        public static boolean Validate(this Master master, 
                                       string type, 
                                       string stringToValdate)
        {
          boolean valid = NO;
          if (type = "date")
          {
              Match m = fechaRegex.Match(stringToValdate);
              if(m.Success)
              {
                    valid = true;
              }
          }
    
         return valid;
        }
    }
    

    Use Case :

    using NamesPaceOfExtension;
    
    Site.Master.Validate("date",TextBox1.Text);