Is there a better way of doing it?

O

Oltmans

Hello all. I'm a novice when it comes to C#. I'm using C# using Visual
Studio 2005 and .NET 2.0. I've a scenario and I thought some smart
people would let me know a better way of doing it. I will appreciate
all the help.

I've defined number of methods like

public Method1(){

}
public Method2(){

}
public Method3(){

}
public Method4(){

}
public Method5(){

}

and I've to call the sequentially like

Method1();
Method2();
Method3();
Method4();
Method5();

Calling them sequentially one by one doesn't seem like a great
solution. Is there a better way of doing it? Can we somehow store
method references in arrays in C# (2.0) and then iterate over them one
by one? Please suggest me a better way of doing this. It has to be in
C# (.NET 2.0). Thanks.
I will really appreciate any help and a fresh perspective on this.
Agan, thanks for the time and help.
 
M

Martin Honnen

Oltmans said:
Calling them sequentially one by one doesn't seem like a great
solution. Is there a better way of doing it? Can we somehow store
method references in arrays in C# (2.0) and then iterate over them one
by one?

Define a Delegate, then you can create an array of them:

public delegate void MyDelegate();

class Program
{
static void Main(string[] args)
{
MyDelegate[] methods = new MyDelegate[] { Message1, Message2 };
foreach (MyDelegate method in methods)
{
method();
}
}

static void Message1()
{
Console.WriteLine("foo");
}
static void Message2()
{
Console.WriteLine("bar");
}
}

That assumes your methods have the same signature.


See http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx
 
J

Jeff Johnson

and I've to call the sequentially like

Method1();
Method2();
Method3();
Method4();
Method5();

Calling them sequentially one by one doesn't seem like a great
solution. Is there a better way of doing it? Can we somehow store
method references in arrays in C# (2.0) and then iterate over them one
by one? Please suggest me a better way of doing this. It has to be in
C# (.NET 2.0). Thanks.

As Martin showed, it CAN be done, but I have to ask, WHY do you want to? Do
you plan on altering the arrays at run-time so that you can call a different
set up methods based on different conditions? If not, and you will ALWAYS be
calling Method1 - 5, then I see the delegate route to be nothing more than
unnecessary complexity. Just make a RunMainProcess() method which calls
those methods like above and be done with it. What you think doesn't look
like "a great solution" looks like "normal, everyday code" to me.
 
P

Pavel Minaev

Hello all. I'm a novice when it comes to C#. I'm using C# using Visual
Studio 2005 and .NET 2.0. I've a scenario and I thought some smart
people would let me know a better way of doing it. I will appreciate
all the help.

I've defined number of methods like

public Method1(){

}

public Method2(){

}

public Method3(){

}

public Method4(){

}

public Method5(){

}

and I've to call the sequentially like

Method1();
Method2();
Method3();
Method4();
Method5();

Calling them sequentially one by one doesn't seem like a great
solution. Is there a better way of doing it?

Well, if you always call them all, and always in that order, then why
not just wrap it into another method, and call that where needed?

Or do you actually want a jump table? In which case, you should use
delegates as described above. Though before defining your own delegate
types, take a look at System.Action<...> and System.Func<...> and see
if those will be sufficient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top