Create array of C# procedures?

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

I want to display a list of various C# procedures in a listbox control. When
the user selects an item (one of the procedures) in the listbox I want to
invoke the selected procedure from the listbox click event. I want to list
each procedure in some type of array where I call the array in the click
event something like this: (pseudo code)

private void listbox1_Click(object sender, EventArgs e)
{
myProcArray(listbox1.SelectedIndex);
}

public void proc1()
{...
}

public void proc2()
{...
}
....

I am hoping to bypass the switch/case construct and just invoke the
procedures from a procedure array. How do I create such an array of
procedures?

Thanks
 
Rich said:
I want to display a list of various C# procedures in a listbox control. When
the user selects an item (one of the procedures) in the listbox I want to
invoke the selected procedure from the listbox click event. I want to list
each procedure in some type of array where I call the array in the click
event something like this: (pseudo code)

private void listbox1_Click(object sender, EventArgs e)
{
myProcArray(listbox1.SelectedIndex);
}

public void proc1()
{...
}

public void proc2()
{...
}
...

I am hoping to bypass the switch/case construct and just invoke the
procedures from a procedure array. How do I create such an array of
procedures?

Define a delegate and build an array of that delegate type. Or in your
case above you could use the predefined Action delegate type e.g.
Action[] procedures = new Action[] { this.proc1, this.proc2 };
then you can do

private void listbox1_Click(object sender, EventArgs e)
{
procedures(listbox1.SelectedIndex);
}

Note that Action is .NET 3.5 but you could define a delegate yourself if
you use earlier versions.
 
Sorry - I meant

private void listbox1_Click(object sender, EventArgs e)
{
myProcArray[listbox1.SelectedIndex];
}
 
Martin said:
Define a delegate and build an array of that delegate type. Or in your
case above you could use the predefined Action delegate type e.g.
Action[] procedures = new Action[] { this.proc1, this.proc2 };
then you can do

private void listbox1_Click(object sender, EventArgs e)
{
procedures(listbox1.SelectedIndex);

Should be
procedures[listbox1.SelectedIndex]();
 
Thanks. That is very cool. May I ask how I would create a delegate for
earlier versions (VS2005)?



Martin Honnen said:
Rich said:
I want to display a list of various C# procedures in a listbox control. When
the user selects an item (one of the procedures) in the listbox I want to
invoke the selected procedure from the listbox click event. I want to list
each procedure in some type of array where I call the array in the click
event something like this: (pseudo code)

private void listbox1_Click(object sender, EventArgs e)
{
myProcArray(listbox1.SelectedIndex);
}

public void proc1()
{...
}

public void proc2()
{...
}
...

I am hoping to bypass the switch/case construct and just invoke the
procedures from a procedure array. How do I create such an array of
procedures?

Define a delegate and build an array of that delegate type. Or in your
case above you could use the predefined Action delegate type e.g.
Action[] procedures = new Action[] { this.proc1, this.proc2 };
then you can do

private void listbox1_Click(object sender, EventArgs e)
{
procedures(listbox1.SelectedIndex);
}

Note that Action is .NET 3.5 but you could define a delegate yourself if
you use earlier versions.
 
Rich said:
Thanks. That is very cool. May I ask how I would create a delegate for
earlier versions (VS2005)?

The definition of that Action delegate in the System namespace is as
follows:

public delegate void Action();

The syntax to define delegate types has not changed, the only difference
in .NET 3.5 is that .NET 3.5 defines some delegate types that are used
often, such as as that Action delegate shown above.
With .NET 2.0 you need to define it yourself respectively you can choose
a different name if you like.
 
Thanks. That is very cool. May I ask how I would create a delegate for
earlier versions (VS2005)?

It's just a normal delegate declaration. Just copy it from the MSDN
documentation for the Action delegate:

public delegate void Action();
 
Rich said:
Thanks. That is very cool. May I ask how I would create a delegate for
earlier versions (VS2005)?

You declare the delegate like this:

public delegate void MyDelegate();

And you can then use MyDelegate instead of Action:

MyDelegate[] procedures = new MyDelegate[] { this.proc1, this.proc2 };

private void listbox1_Click(object sender, EventArgs e)
{
procedures[listbox1.SelectedIndex]();
}

Note that the signature of MyDelegate (in the example it takes no
parameters and returns void) has to match the signature of procedures proc1
and proc2.
 
Back
Top