How to make method from a class to array?

Currently I'm making a quiz game in C # Console Application. And I also put the quiz questions in each method in a class. However, I have random quizzes so that when the user playing the game again, they do not meet the same quiz that sequence as before. My problem here is how to create a method in a class in the Array to another class? And I want that when I call a method in one of Quiz class Quiz, will be randomized.

class Quiz
{
    public static void Quiz1()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("What is this?");
        Console.WriteLine("│ .. / .- -- / .--. .-. --- --. .-. .- -- -- . .-.");
        Console.WriteLine("│ (hint: sound)");

        Text.Answer(2, 7, 2, 8, "I AM PROGRAMMER", "I am programmer", "i am programmer", 3); //This method from Text class  
    }

    public static void Quiz2()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("2 - 4 - 12 - 44 - ?");

        Text.Answer(2, 6, 2, 7, "172", "172", "172", 3); //This method from Text class
    }

    public static void Quiz3()
    {
        Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("If MACHINE is LBBIHOD");
        Console.WriteLine("│ So PROGRAM is....");

        Text.Answer(2, 7, 2, 8, "OSNHQBL", "osnhqbl", "Osnhqbl", 3); //This method from Text class
    }
    //Sorry I just copy 3 question. Because I have so many question XD
}

From this class I want call the method from Quiz class

class Text
{
    public static void Word(string Teks, int x, int y, ConsoleColor wt, ConsoleColor wb)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = wt;
        Console.BackgroundColor = wb;
        Console.Write(Teks);
    }

    public static void Answer(int x1, int y1, int x2, int y2, string correctanswer1, string correctanswer2, string correctanswer3, int count)
    {
        //Quiz[] kuis = {Quiz1(), Quiz2(), Quiz3()}
        //I TRY TO MAKE LIKE ABOVE BUT MAYBE IT'S NOT LIKE THAT

        Console.SetCursorPosition(x1, y1);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Your answer : ");

        string answer;
        int chance = 0;
        do
        {
            Console.SetCursorPosition(x2, y2);
            Console.WriteLine("                                                                          ");
            Console.SetCursorPosition(x2, y2);
            answer = Console.ReadLine();
            chance++;
            if (answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("Sorry, your answer is incorrect...");
                Console.Beep();
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey(true);
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("                                   ");
            }
        } while (chance < count && answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3);

        if (answer == correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(x2, y2 + 1);
            Console.WriteLine("Congratulation! Your answer is correct...");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey(true);
            //SO, FROM THIS LINE I WANNA CALL 1 METHOD FROM QUIZ CLASS RANDOMLY
        }
        else
        {
            GameOver();
        }
    }

Thanks for your help. :)

Answers


The following is just to give you an idea of how it could work, I used a List of Action delegates and called them randomly.

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Quiz quiz = new Quiz();
        quiz.Quiz1();
    }
}

class Quiz
{

    List<Action> methods = new List<Action>();//List of action delegates
    Random random = new Random();//Random instance

    public Quiz()
    {
       //Populate the List with your methods
        methods.Add(()=>Quiz1());
        methods.Add(()=>Quiz2());
        methods.Add(()=>Quiz3());
    }

    public void Quiz1() //Removed Static
    {
       // Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("What is this?");
        Console.WriteLine("│ .. / .- -- / .--. .-. --- --. .-. .- -- -- . .-.");
        Console.WriteLine("│ (hint: sound)");

        if (Text.Answer(2, 7, 2, 8, "I AM PROGRAMMER", "I am programmer", "i am programmer", 3)) //This method from Text class  
        {
            int r = random.Next(methods.Count());//Get a random number
            methods[r]();//Call the method using the random number
        }
        else
       {
       GameOver();
       }
    }

    public void Quiz2()
    {
      //  Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("2 - 4 - 12 - 44 - ?");

        if (Text.Answer(2, 6, 2, 7, "172", "172", "172", 3)) //This method from Text class
        {
            int r = random.Next(methods.Count());//Get a random number
            methods[r]();//Call the method using the random number
        }
   else
  {
  GameOver
  }
    }

    public void Quiz3()
    {
      //  Program.DefaultMethod(2, 3); //This method from Program class

        Console.WriteLine("If MACHINE is LBBIHOD");
        Console.WriteLine("│ So PROGRAM is....");

        if (Text.Answer(2, 7, 2, 8, "OSNHQBL", "osnhqbl", "Osnhqbl", 3)) //This method from Text class
        {
            int r = random.Next(methods.Count());
            methods[r]();

        }
    }
    //Sorry I just copy 3 question. Because I have so many question XD
}

class Text
{


    public static void Word(string Teks, int x, int y, ConsoleColor wt, ConsoleColor wb)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = wt;
        Console.BackgroundColor = wb;
        Console.Write(Teks);
    }
    //Now your Answer method returns a bool indicating if the user will continue answering more questions or is it GameOver
    public static bool Answer(int x1, int y1, int x2, int y2, string correctanswer1, string correctanswer2, string correctanswer3, int count)
    {
        //Quiz[] kuis = {Quiz1(), Quiz2(), Quiz3()}
        //I TRY TO MAKE LIKE ABOVE BUT MAYBE IT'S NOT LIKE THAT

        Console.SetCursorPosition(x1, y1);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Your answer : ");

        string answer;
        int chance = 0;
        do
        {
            Console.SetCursorPosition(x2, y2);
            Console.WriteLine("                                                                          ");
            Console.SetCursorPosition(x2, y2);
            answer = Console.ReadLine();
            chance++;
            if (answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("Sorry, your answer is incorrect...");
                Console.Beep();
                Console.ForegroundColor = ConsoleColor.White;
                Console.ReadKey(true);
                Console.SetCursorPosition(x2, y2 + 1);
                Console.WriteLine("                                   ");
            }
        } while (chance < count && answer != correctanswer1 || answer != correctanswer2 || answer != correctanswer3);

        if (answer == correctanswer1 || answer != correctanswer2 || answer != correctanswer3)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(x2, y2 + 1);
            Console.WriteLine("Congratulation! Your answer is correct...");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey(true);

            return true;//Return to Quiz Class
        }
        else
        {
            return false;

            // GameOver();
        }
    }
}
}

Use an iterator like foreach or for to build your array; however, in your case it doesn't look like you are storing values; rather you are setting the values of textboxes, so you can just call your methods which return voids.


Need Your Help

Serilize DataTable (Dataset) to Object

c# xml serialization datatable dataset

I have one class and one query from the database, filling DataTable in the DataSet. Be serialized Datatable to fill (Object) Marks.

the div tag is displaying when refreshing

php javascript html

In this below code i have text box and submit button .when i enter or refresh that page the div tag displays but my expected result is when i enter a value in textbox and click submit button then ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.