locked
Array of Function Pointers? RRS feed

  • Question

  • Hello everybody!  I'm new to C# so I just had a small question.  I know this can be done in other languages so I thought I would ask here and see what the best method would be to do this in C#.  I have a growing number of methods that perform certain functions.  I was trying to just throw all the method names into an array and then loop through the array and use it as a function pointer to call the method.  I've illustrated with the code below. 


    The operation of the while loop is the portion I am focusing in on.  I am thinking it should be relatively easy so if any one could give any advice, I'd appreciate it.


    Thanks.  Chester


        1 using System;

        2 using System.Collections.Generic;

        3 using System.Linq;

        4 using System.Text;

        5 

        6 namespace functionPointerTest

        7 {

        8     class Program

        9     {

       10         static void Main(string[] args)

       11         {

       12             string[] functions = { "dothis()", "dothis1()"};

       13             bool success = true;

       14             int i = 0;

       15 

       16             while (success && i <= functions.Length)

       17             {

       18                 success = functions[i];

       19             }

       20         }

       21 

       22         public bool dothis()

       23         {

       24             Console.WriteLine("dothis");

       25             return true;

       26         }

       27 

       28         public bool dothis1()

       29         {

       30             Console.WriteLine("dothis1");

       31             return false;

       32         }

       33 

       34         public bool dothis2()

       35         {

       36             Console.WriteLine("dothis2");

       37             return true;

       38         }

       39     }

       40 }

    Tuesday, December 9, 2008 4:08 PM

Answers

  • The simple answer is to have an array of delegates. 



    class Program  
    {  
        static void Main ( string[] args )  
        {     
            Func<bool>[] functions = { dothis, dothis1, dothis2 };  
                    foreach (var function in functions)  
                    {  
                        if ( !function () )  
                                break;  
                    }  
     
            }  
     
            public static bool dothis ()  
            {  
                    Console.WriteLine ( "dothis" );  
                    return true;  
            }  
     
            public static bool dothis1 ()  
            {  
                    Console.WriteLine ( "dothis1" );  
                    return false;  
            }  
     
            public static bool dothis2 ()  
            {  
                    Console.WriteLine ( "dothis2" );  
                    return true;  
            }  

    If you want to use names, then you can put the delegates in a Dictionary<string, Func<bool>, and then do something like:

    funcDictionary["dothis"](); 

    HTH
    • Edited by Tom Shelton Tuesday, December 9, 2008 4:38 PM code fix
    • Marked as answer by Chester666 Tuesday, December 9, 2008 4:53 PM
    Tuesday, December 9, 2008 4:33 PM

All replies

  • The simple answer is to have an array of delegates. 



    class Program  
    {  
        static void Main ( string[] args )  
        {     
            Func<bool>[] functions = { dothis, dothis1, dothis2 };  
                    foreach (var function in functions)  
                    {  
                        if ( !function () )  
                                break;  
                    }  
     
            }  
     
            public static bool dothis ()  
            {  
                    Console.WriteLine ( "dothis" );  
                    return true;  
            }  
     
            public static bool dothis1 ()  
            {  
                    Console.WriteLine ( "dothis1" );  
                    return false;  
            }  
     
            public static bool dothis2 ()  
            {  
                    Console.WriteLine ( "dothis2" );  
                    return true;  
            }  

    If you want to use names, then you can put the delegates in a Dictionary<string, Func<bool>, and then do something like:

    funcDictionary["dothis"](); 

    HTH
    • Edited by Tom Shelton Tuesday, December 9, 2008 4:38 PM code fix
    • Marked as answer by Chester666 Tuesday, December 9, 2008 4:53 PM
    Tuesday, December 9, 2008 4:33 PM
  • Thanks Tom.  Looks like I need to look more closely at those delegates!

    Thanks again.
    Tuesday, December 9, 2008 4:55 PM
  • Is this just something that works in .NET 3.x and higher?  I'm programming my application for .NET 2.0 and I'm getting this error:

    The type or namespace name 'Func' could not be found (are you missing a using directive or an assembly reference?)

    In which I think System.Linq is not available yet?

    Wednesday, December 17, 2008 3:34 PM
  • It's in the System.Core.dll... Not sure which version it ships with, but I strongly believe it came with .NET 3.0 or 3.5...
    Wednesday, December 17, 2008 3:55 PM
  • No worries, though...
    You can just create your own Func delegate as long as you're on the other frameworks:

    public delegate T Func<T>();

    That's it... Now you can go and create your own Func<bool>[] :D
    Wednesday, December 17, 2008 3:58 PM
  • As echom pointed out, Func is a delegate that is part of the core framework.  It was added in 3.5, so if you using an earlier version of .NET then you won't have it.  I just used it because it was convienient.  The technique works with any delegate type.  So, you can just create your own delegate and use that.

    Sorry, it that created any confusion.


    Tom Shelton
    Wednesday, December 17, 2008 4:22 PM