• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Duplicate Code

I was wondering basically how I could go about duplicating the function of this code in C#.  The following is C code that stores a pointer to a subrouting in a variable so that it may be called later in the code.  Ideally with C#, I would like to store the pointer to the subrouting in an objects tag property and then execute the required subrouting based on the tag property.  Can someone give me some insight on how to correctly duplicate this in C#.
 
int (*validate_column)(void) = NULL;
 
setup_columns ( int col )
    {
      if ( col == 1 )
          validate_column = test_column_1;
    else if  ( col == 2 )
          validate_column = test_column_2;
    else
          validate_column = NULL;
    }
(int) test_column_1 ( void )
    {
    // Validate Data for Column 1
    }
(int) test_column_2 ( void )
    {
    // Validate Data for Column 2
    }
main ()
    {
        int col, ier;
        col = 1;
        setup_columns ( col );
        if ( (*validate_column) != NULL )
            ier = (*validate_column)();
    }

Thanks,
Kendal
0
gvector1
Asked:
gvector1
  • 3
  • 2
1 Solution
 
fromerojCommented:
// Declare a delegate type for processing
public delegate int validate_column();

public class myClass
{
setup_columns ( int col )
}
0
 
fromerojCommented:
// Declare a delegate type for processing
public delegate int validate_column();

public class myClass
{
     delegate myValidation;

    public int test_column_1()
    {
      // Validate Data for Column 1
     }
    public int test_column_2()
    {
      // Validate Data for Column 2
     }

     public void setup_columns( int col )
     {
       switch(col):
        {
           case 1:
               myValidation= new validate_column(test_column_1);
               break;
          case 2:
              myValidation= new validate_column(test_column_2);
              break;
          default:
               break;
     }

     public void Main()
    {
        int col, ier;
        col = 1;
        setup_columns( col );
        if ( (myValidation) != NULL )
           myValidation();
    }
}
0
 
fromerojCommented:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ptmcompCommented:
You could build a hashmap (or array) with the col number and the delegate to the specific validation method.
0
 
ptmcompCommented:
Or you can build a set of validation classes which can be customized. Create a validation object per validation kind and then fill them in a hashmap or array and call them using a common interface/class method.
0
 
gvector1Author Commented:
Fromeroj - I have not completely confirmed that that will solve my problem, but I believe it will work.  

Thanks,
Kendal
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now