Hello,
I have to finish my assignment.
I have different console exercises. I can solve it.
but I have to combine them so that they will execute one after another.
I do not know how to do that.
in my exercise, I make one program to declare class and other to test it. i.e. application program.
I have 2/3 exercises like this.
If I copy and paste them one after another it gives error having more than one main method.
Please help me.
thank you.

I'm not 100% sure I understand, but what I think you're trying to say is: you have 2 (or 3) programs (console apps), that do all of their stuff in "main()". You need to combine them into one program, so you can run it and it will do all 2 (or 3) things.

If that's the case, first, in each project, create a function and move all of the code from "main" into it. Then, from "main", all you're doing is calling the function that does all of your stuff. Example:

Old way:

class Program
{
    static void Main(string[] args)
    {
       int x = 3;
       int y = 5;
       Console.Out.WriteLine("The total is: {0}", (x + y));
    }
}

New way:

class Program
{
    public void doFirstThing()
    {
       int x = 3;
       int y = 5;
       Console.Out.WriteLine("The total is: {0}", (x + y));
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.doFirstThing();
    }
}

Once you have that, you can create a new project that has all 2 (or 3) of your functions in it, then your "Main" looks like this:

static void Main(string[] args)
{
    Program p = new Program();
    p.doFirstThing();
    p.doSecondThing();
    p.doThirdThing();
}

Hope this helps!

Thank you Mike.
That really worked.

If I want to use those programs as different classes, how can I do it?

Implement each function in it's own class. But the structure of the program would change a bit.

First, create a new .cs file - you will put your function in this file (for example, call it "FirstClass.cs"):

public class FirstClass
{
    public FirstClass()
    {
        // default constructor
    }

    public void doFirstThing()
    {
        int x = 3;
        int y = 5;
        Console.Out.WriteLine("The total is: {0}", (x + y));
    }
}

Then, in your main, you would do this:

FirstClass oFC = new FirstClass();
oFC.doFirstThing();

You can create new .cs files for your other functions, and use the same convention as above to include them and call them in your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.