tags:

views:

78

answers:

1
+2  Q: 

Invoking methods

Hi,

Now that I have a better grasp of Classes and their importance in C#, I have a question about how to handle a certain situation.

I have to perform 5-6 tests using 3 different external devices that all require about 10 or so commands to be setup at the begining of each test.

So there are approx 10 commands for each device for 6 tests.

I was originally going to but these into an array for each device but now it makes more sense to have a method in the Class for each deveice for each step - device1_test1() device2_test2() etc.

This is fine but I need to give the user the ability to go back to a previous step incase of an error.

So if the error occurs at TEST4, the user can go back to TEST2, but I obviously need to reissue the commands so that I am sure where we are since the commands depend on each other as they progress thru the tests.

So I would issues test1 commands for all devices then test2 commands for all devices and so on.

Is this how I should be doing it ?

If so, when I invoke device1_test1 and 2 etc, can I do this by using the contents of a variable in the method name for the test1 test2 test3 part of it by incrementing an integer ?

I am not sure of the syntax even but something like this -

 device1_test+counter+() where counter will contain 1, then 2, then 3 etc ?

Hope this makes sense.

Otherwise, any suggestions on how to better tackle this scenario ?

Thanks, George.

+4  A: 

Delegates will help you here.

Action[] testCases = new Action[] { device1_test1, devide1_test2 };

And then just iterate over the collection with a usual for loop:

for(int i = 0; i < actions.Length; ++i) {
    try {
        actions[i].Invoke();
    }

    catch(TestFailedException) {
        if(i > 1)
            i -= 1;
    }
}
Anton Gogolev
Hi Anton,Thanks for that, its exactly what I was after !Cheers, George.
George