Solved

Syntax to name class method from data element in foreach loop

Posted on 2013-06-27
1
289 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now