Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Syntax to name class method from data element in foreach loop

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

790 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