Ronald Gans

Ronald Gans

  • 1.6k
  • 12
  • 5.4k

Delegate array failure

Apr 19 2013 9:41 AM
In order to dispatch functions based on values I initialize an array of delegates. This has worked quite well for a while. The functions all have the same signature:

void function01 (ref byte[] buff, int k);


I have about 26 of them, function01, function02, function03, etc.


I declare the delegate this way:


 delegate void Function(ref byte[] inB, int i);


and the array this way:


Functions[] Functions= new Functions[256];


Then I initialize the array


for (i=0; i<256;i++)
if (i < 10) Functions[i] = new Function(Function01);
        if (i <=11 && i < 20) Functions[i] = 


etc.


This works quite well for me; maybe there's a better way but this works. And it works for all my functions 01-26. However, within the body of a few of them, I make a call to another function. This doesn't (to my thinking) affect the signature of the function. What does it matter what happens within the body of the function.




public void Function10(ref byte[] inB, int k)
        {
           
           
            for (int i = 0; i < inB.Length; i++)
            {
               
                byte[] values = selectBy(ref inB, Key[i]); //<<<====THIS IS THE PROBLEM
                 
                returnBy(ref inB, ref values, Key[i]);
            }
             
           
        }

If I comment out all the code in Function10, there is no problem. When I call my dispatch function


 for (j = 0; j < inB.Length; j++)
  {
                    Functions[Key[j]](ref inB, j);


   }



(Key[j] is guaranteed to be 0-255), there is no problem. BUT if i uncomment  the offending lines, I get array out of bounds error.


When I single-step, I can see the delegate array and there is no error at all. WHY should the code in the body of the function make a difference to its signature? Perhaps the error is somewhere else? This started recently, naturally.

I GUESS the basic question is: If I have an array of delegates for all of which the underlying functions have the same signature, why does it matter what the body of function is? 

RON





Answers (2)