Link to home
Start Free TrialLog in
Avatar of Steve7423
Steve7423Flag for Canada

asked on

Syntax to name class method from data element in foreach loop

Ok this is a strange one.  I need to find the syntax to convert a data element to a class function, I’ll explain.

I have a class called Validation, and within that class there are many functions Validate_Client_Name (string value) and Validate_Client_Address (string value) so on.

In the interest of efficiency and tight code I’m trying to reduce the amount of lines by using a foreach rather than 100 if statements.  Different clients could have a combination of different validation functions that need to be done.  Not only that I’d like to make it easy for the users so they can turn on and off options based on the client.

Eg: Client A can use validate_name while Client B could use Validate_Address and Client C could use Validiate_Name and Validate_Address.

The object of the game is to loop through the validation records which are named the same as the functions.  This way only the selected validation selections for the client are performed.

This week the users could decide to use the validate client_Name on Client A and then next week they could turn that off and use Validate_Client_Address.  The Data Access Layer returns a list of the functions based on a sql proc with parameters.

The problem is, when you instantiate the Validation class: Validation valMgt = new Validation () I would like to somehow use the value in the foreach variable as the name of the function.

The problem line is valMgt.Opt.sOption_Name(_value);

What it should be is valMgt.Validate_Client_Name(value)

The value in the table is “Validate_Client_Name” which corresponds to the function name

Looking at the foreach loop you can see how I’m trying to solve it but I’m stuck on the syntax or if it can even be done.

I’ve tried:
      valMgt+”.”+ Opt.sOption_Name +”(“+value+”)”  along with a few other things and nothing works.

Any ideas???



Public class Validation
{
           public string Validate_Client_Name(string value)
           {
           }

           public string Validate_Client_Address(string value)
           {
           }
}


Public class Process
{
        Public void Processing_Client
        {      
                DataAccessLayer DAL = new DataAccessLayer()

                foreach (var Opt in DAL.Get_Validation_Options("client").ToList())
                {
                      int[] sysOptions = new int[Opt.Proj_ValidOpt_ID];
                      sysOptions[Opt.Proj_ValidOpt_ID] = valMgt.Opt.sOption_Name(_value);
                      if (sysOptions[Opt.Proj_ValidOpt_ID].ToString() == "Fail")
                 {
                         Msg = “problem validating “ + SysOptions[Opt.Proj_ValidOpt_ID].ToString()
                         Break;
                      }
               }
      }
}

SQL table:

Tbl_System_Validation

Sys_Val_ID                      (key)
Validation_Name               varchar(50)

Data:

Sys_Val_ID    Validation_Name            
1      Validate_Client_Name
2      Validate_Client_Address
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial