Solved

Syntax to name class method from data element in foreach loop

Posted on 2013-06-27
1
296 Views
Last Modified: 2013-07-18
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
0
Comment
Question by:Steve7423
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39283387
Honestly, this sounds like a brittle design, and I fear you may have more problems than you'd like down the line. That said, you can dynamically invoke a function by name through reflection. You will incur a performance hit. It may or may not be noticeable, depending on how often this code is running.

Before I demonstrate reflection, I wonder if I might propose the following instead. What if you keep a dictionary of string to delegates. You could populate the dictionary with the string names that you want to identify the functions by, and then index the dictionary using the target string to retrieve the designated delegate (read function) to execute.

For example:

Dictionary<string, Func<string, string>> funcMap = new Dictionary(string, Func<string, string>>();

funcMap.Add(“Validate_Client_Name”, valMgt.Opt.Validate_Client_Name);
funcMap.Add(“Validate_Client_Address", valMgt.Opt.Validate_Client_Address);

Open in new window


...Then:

sysOptions[Opt.Proj_ValidOpt_ID] = funcMap[Opt.Proj_ValidOpt_ID]();  // call mapped function

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question