2

Program1.cs Regular C# file, works perfectly.

Random numberGen = new Random();

int roll1 = 1;
int roll2 = 0;
int roll3 = 0;
int roll4 = 0;

int attempts = 0;

Console.WriteLine("Press enter to roll the dies");

while (roll1 != roll2 || roll2 != roll3 || roll3 != roll4 || roll4 != roll1)
{
    Console.ReadKey();

    roll1 = numberGen.Next(1, 7);
    roll2 = numberGen.Next(1, 7);
    roll3 = numberGen.Next(1, 7);
    roll4 = numberGen.Next(1, 7);
    Console.WriteLine("Dice 1: " + roll1 + "\nDice 2: " + roll2 + "\nDice 3: " + roll3 + "\nDice 4: " + roll4 + "\n");
    attempts++;
}

Console.WriteLine("It took " + attempts + " attempts to roll a four of a kind.");

Console.ReadKey();

Program2.cs

Console.ReadKey();

Under the module Console it pops up an error: Only one compilation unit can have top-level statements. Error: CS8802

I tried in the terminal dotnet new console --force, but it just ends up deleting my program. I want to run multiple C# files in the same folder without getting the Only one compilation unit can have top-level statements or other similar errors.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Canyon
  • 61
  • 1
  • 6
  • 1
    You want to run multiple programs at the same time? Both of them read console input. How would that work? – Charles Mager Apr 10 '22 at 10:57
  • 1
    Isn't the error message pretty clear? You only get one file where you can dispense with the trouble of defining a class. – Kirk Woll Apr 10 '22 at 11:12
  • Does it mean i have to make a new folder for my C# files? – Canyon Apr 10 '22 at 11:14
  • @Canyon a folder would make no difference. What are you actually trying to do here? You can define anything else in another file (e.g a class), but what you're effectively doing above is defining two `Main` methods / entry points. You can only have one of those, executing more than one makes no sense. – Charles Mager Apr 10 '22 at 11:29

1 Answers1

3

In dotnet 6, you do not need to have a class name for the main method.

So when you have 2 class that does not have class and namespace, the compiler thinks you have 2 main methods.

So you do something like

namespace ConsoleApp1;

class Program1
{
    public static void GetRolling()
    {
        Random numberGen = new Random();

        int roll1 = 1;
        int roll2 = 0;
        int roll3 = 0;
        int roll4 = 0;

        int attempts = 0;

        Console.WriteLine("Press enter to roll the dies");

        while (roll1 != roll2 || roll2 != roll3 || roll3 != roll4 || roll4 != roll1)
        {
            Console.ReadKey();

            roll1 = numberGen.Next(1, 7);
            roll2 = numberGen.Next(1, 7);
            roll3 = numberGen.Next(1, 7);
            roll4 = numberGen.Next(1, 7);
            Console.WriteLine("Dice 1: " + roll1 + "\nDice 2: " + roll2 + "\nDice 3: " + roll3 + "\nDice 4: " + roll4 + "\n");
            attempts++;
        }

        Console.WriteLine("It took " + attempts + " attempts to roll a four of a kind.");
    }
}

and for program2 some think like:

namespace ConsoleApp1;

public class Program2
{
    public static void Main(string[] args)
    {
        Program1.GetRolling();
        Console.ReadKey();
    }
}

Otherwise, it is like saying have 2x public static void Main(string[] args) and that is not possible.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137