?
Solved

Duplicate Code

Posted on 2005-03-17
6
Medium Priority
?
245 Views
Last Modified: 2012-05-05
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
Comment
Question by:gvector1
[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
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:fromeroj
ID: 13568020
// Declare a delegate type for processing
public delegate int validate_column();

public class myClass
{
setup_columns ( int col )
}
0
 
LVL 2

Accepted Solution

by:
fromeroj earned 2000 total points
ID: 13568117
// 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
 
LVL 2

Expert Comment

by:fromeroj
ID: 13568127
0
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!

 
LVL 10

Expert Comment

by:ptmcomp
ID: 13586342
You could build a hashmap (or array) with the col number and the delegate to the specific validation method.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 13586352
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
 

Author Comment

by:gvector1
ID: 13672005
Fromeroj - I have not completely confirmed that that will solve my problem, but I believe it will work.  

Thanks,
Kendal
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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