?
Solved

Syntax to name class method from data element in foreach loop

Posted on 2013-06-27
1
Medium Priority
?
299 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
[X]
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
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 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

Technology Partners: 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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