❔ top-level statement

I have a folder where I have 1 cs file and it is working perfect. I want to start working on a new CS file within the same folder and when I try to compile, then it says " Only one compilation unit can have top-level statements". How can I solve this issue?
21 Replies
phaseshift
phaseshift16mo ago
You need to write classes etc
SinFluxx
SinFluxx16mo ago
Top-level statements - C# tutorial
This tutorial shows how you can use top-level statements to experiment and prove concepts while exploring your ideas
phaseshift
phaseshift16mo ago
You generally cannot just write code that 'does stuff' outside of a method, outside of a class.
The one file allowed for top level statements is an exception
Bilal Hameed (Nebb engineering)
ok, I need to make method and class?
SinFluxx
SinFluxx16mo ago
Yes, essentially you can only have one file that doesn't contain the class and method declarations
Bilal Hameed (Nebb engineering)
using System;

public class Program
{
public class Calculator
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}

static void Main()
{
Calculator calculator = new Calculator();
int result = calculator.Add(5, 10);
Console.WriteLine("The sum is: " + result);
}
}
using System;

public class Program
{
public class Calculator
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}

static void Main()
{
Calculator calculator = new Calculator();
int result = calculator.Add(5, 10);
Console.WriteLine("The sum is: " + result);
}
}
the console output says vthat I am missing partial modifier on declaration of type 'program' i have tried to make an file with method and class (help from ChatGPT), but it doesnt work can we have an online session?
SinFluxx
SinFluxx16mo ago
Bilal Hameed (Nebb engineering)
but this is the top-level statement file
SinFluxx
SinFluxx16mo ago
Yes, does that mean you have another file?
SinFluxx
SinFluxx16mo ago
what's in that file?
Bilal Hameed (Nebb engineering)
i have 2 cs files
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
test.method();
// Your code goes here

if (daysUntilExpiration > 11)
{
Console.WriteLine("Your subscription will expire soon. Renew now!");
}
else if (daysUntilExpiration > 5)
{
discountPercentage = 10;
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days. Renew now and save {discountPercentage}%!");
}
else if (daysUntilExpiration > 1)
{
discountPercentage = 20;
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days. Renew now and save {discountPercentage}%!");
}
else if (daysUntilExpiration == 1)
{
discountPercentage = 20;
Console.WriteLine("Your subscription expires within a day! Renew now and save {discountPercentage}%!");
}
else
{
Console.WriteLine("Your subscription has expired.");
}


public class class2 {
{

}

}
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
test.method();
// Your code goes here

if (daysUntilExpiration > 11)
{
Console.WriteLine("Your subscription will expire soon. Renew now!");
}
else if (daysUntilExpiration > 5)
{
discountPercentage = 10;
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days. Renew now and save {discountPercentage}%!");
}
else if (daysUntilExpiration > 1)
{
discountPercentage = 20;
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days. Renew now and save {discountPercentage}%!");
}
else if (daysUntilExpiration == 1)
{
discountPercentage = 20;
Console.WriteLine("Your subscription expires within a day! Renew now and save {discountPercentage}%!");
}
else
{
Console.WriteLine("Your subscription has expired.");
}


public class class2 {
{

}

}
this is my main file
SinFluxx
SinFluxx16mo ago
When you have a file that removes the top level statements, that file is assumed to be class Program with a static void Main, so you shouldn't be use that class name in your file that does have top level statements. Also, if these files are completely disconnected then you should probably put them in different Projects
Bilal Hameed (Nebb engineering)
So I need to make a folder everytime I want to a new cs file?
SinFluxx
SinFluxx16mo ago
No, just don't have a file without top level statements, and then create another file with a class Program in it as well, as these will conflict Moving them into separate projects was just a suggestion based on them seeming to be completely unrelated functionality essentially the Main method in your Program class is the entry point for your application, i.e. where the code will start executing from, so you can only have one of these, at this point in time it might be easier to NOT exclude top level statements as it can lead to confusion
Bilal Hameed (Nebb engineering)
so I should include top level-statement
SinFluxx
SinFluxx16mo ago
You don't have to, I just think it helps to avoid confusion when you're starting out
Bilal Hameed (Nebb engineering)
i understand
anita
anita16mo ago
If you just want to create one file scripts then i can recommend https://github.com/dotnet-script/dotnet-script
GitHub
GitHub - dotnet-script/dotnet-script: Run C# scripts from the .NET ...
Run C# scripts from the .NET CLI. Contribute to dotnet-script/dotnet-script development by creating an account on GitHub.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server