How do I fix the top-level statement's error?
Asked Answered
S

1

5

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 ended 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.

Sharpie answered 10/4, 2022 at 10:45 Comment(4)
You want to run multiple programs at the same time? Both of them read console input. How would that work?Naarah
Isn't the error message pretty clear? You only get one file where you can dispense with the trouble of defining a class.Grinnell
Does it mean i have to make a new folder for my C# files?Sharpie
@Sharpie 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.Naarah
Z
6

In dotnet 6 and newer, it is possible to use a top-level statement, which means you do not need to have a class name for the main method.

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

So you can eventually do something like for Program1.cs:

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.cs something like:

namespace ConsoleApp1;

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

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

Zerlina answered 10/4, 2022 at 11:47 Comment(4)
How do i choose which Program i want to run?Sharpie
the program that contain method Main, So Main is the entry point for your program. Program is just a class,Zerlina
I will change my answer to give you better pictureZerlina
so now program1 contains a method with some logic, and program 2 calling a method in program1, Program 2 have the Main method that is the entry point for startingZerlina

© 2022 - 2024 — McMap. All rights reserved.