Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
public static void Main(string[] args)
       {
           for (int i = 1; i<=5; i++)
           {
                   Journali();
           }
       }

public static void Journal1()
       {
           console.write("a");
       }
public static void Journal2()
       {
           console.write("b");
       }
public static void Journal3()
       {
           console.write("c");
       }


is there any way like described abowe to call multiple methods using loop.
Posted

Yes, with the power of delegates:
C#
class Program
{
  delegate void JournalMethod();

  public static void Journal1()
  {
    Console.Write("a");
  }
  public static void Journal2()
  {
    Console.Write("b");
  }
  public static void Journal3()
  {
    Console.Write("c");
  }

  static void Main(string[] args)
  {
    JournalMethod[] jm = { new JournalMethod(Journal1), new JournalMethod(Journal2), new JournalMethod(Journal3) };
    for (int n = 0; n < 3; ++n)
      jm[n]();
  }
}
 
Share this answer
 
Comments
BillWoodruff 14-Jan-16 11:11am    
+5 While I think this is probably overkill for where the OP is at, it's creative !
CPallini 14-Jan-16 11:45am    
Thank you.
Sergey Alexandrovich Kryukov 14-Jan-16 11:59am    
Sure, a 5. I believe this is what the inquirer was looking for.
—SA
If I assume your goal here is to come up with a way to call various methods using a selector argument:
C#
using System;
using System.Collections.Generic;

namespace YourNameSpace
{
    public static class TestClass
    {
        static TestClass()
        {
            IntToAction = new Dictionary<char, Action>
            {
                {'a', Journal1},
                {'b', Journal2},
                {'c', Journal3}
            };
        }

        public static Dictionary<char, Action> IntToAction; 

        // 'args set to null here so they can be ignored
        public static void Test(string[] args = null)
        {
            for (char theChar = 'a'; a < 'd'; a++)
            {
                // invoke the method selected by the char Key
                IntToAction[theChar]();
            }
        }

        public static void Journal1()
        {
            Console.WriteLine("a");
        }

        public static void Journal2()
        {
            Console.WriteLine("b");
        }

        public static void Journal3()
        {
            Console.WriteLine("c");
        }
    }
}
This example of "method dispatch" uses a Dictionary where the Key is Type 'char, and the Value is Type Action (with no parameters). Dictionaries in .NET provide very efficient look-up.

'Action is a type of Delegate that allows a more terse declarative syntax. See the solution here by C. Pallini for an example of using Delegates.
 
Share this answer
 
v3
What exactly is wrong with the obvious version?
C#
public static void Main(string[] args)
{
    Journal1();
    Journal2();
    Journal3();
}
 
public static void Journal1()
{
    console.write("a");
}
public static void Journal2()
{
    console.write("b");
}
public static void Journal3()
{
    console.write("c");
}
Please edit your question to make more clear what you're trying to prevent.
 
Share this answer
 
Comments
BillWoodruff 14-Jan-16 11:11am    
+5 This is the right question to ask.
You could invoke multiple methods in a loop using reflection.
However, your entire design is wrong here.

These methods do the same thing i.e. print a character.
Thus, you should have a single method and then call that method with a parameter.

Something like
C#
public static void Main(string[] args)
       {
           for (int i = 1; i<=5; i++)
           {
                   Journal(i);
           }
       }
 
public static void Journal(int count)
       {
           if (count == 1)
           {    
             console.write("a");
           } else if (count == 2) {
             console.write("b");
           } // and so on. Better to use a switch case rather than if
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900