micah1211 micah1211 - 2 months ago 8
C# Question

Running another .cs file within the main program.cs file

I'm very new to programming in general so I'm sure this question is naive.

So, I wrote a simple magic eight ball program and I wanted to move onto an adventure game, but I thought it would be neat to include the eight ball program in the game, like making a console menu where you could select either game.

So, my problem is, I don't know how to put my eightball program in a file, and then run it from the main program file with something like

eightball();


And if this can be done simply, could I also use it in an if statement, like

if (response == 1)
{
eightball();
}


Thank you all for your help!

Also I'm sure I ran across the answer in my research, however I probably didn't understand it.

Answer

Get a look at classes.

You will probably want to create a new class file and then add your code something like this:

namespace MyApplication
{
    class EightBall
    {
        public static void eightball ()
        {
            // Add your code here
        }
    }
}

Call it from your Main-function like this:

if (response == 1)
{
    EightBall.eightball();
}