C
C#3y ago
EliasGPS

Only one compilation unit can have top-level statements. [Project]csharp(CS8802) [Answered]

I want to make my code cleaner by making different files. But when i make different files i get this error. Any help?
15 Replies
EliasGPS
EliasGPSOP3y ago
if i write this code in my other file than there is no problem but i want different files to make my code cleaner
cap5lut
cap5lut3y ago
then use regular classes instead of top-level statements in the other files
EliasGPS
EliasGPSOP3y ago
uhh what? i'm new to c# soory
cap5lut
cap5lut3y ago
top-level statements are basically just there to remove some boiler plate
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
is just short for
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
EliasGPS
EliasGPSOP3y ago
oh ok how can i use regular classes than?
V0FBU1VM
V0FBU1VM3y ago
C# only allows top level in Program.cs.
cap5lut
cap5lut3y ago
so if u would want to split up ur code for readability u would have to write more methods/classes. it highly depends on the nature of ur application how to split it up semantically
V0FBU1VM
V0FBU1VM3y ago
You have to put your code inside classes.
EliasGPS
EliasGPSOP3y ago
yeah yeah i understand is there an externsion is vscode to beautify my code
V0FBU1VM
V0FBU1VM3y ago
You can reformat using shit alt f You should be good to go if you have the c# extension installed.
cap5lut
cap5lut3y ago
basically methods/classes are grouping some logic together, eg, one to read data from a website, another to process that data and a third to give some output. ur Main method (or Program.cs' top level statements) would simply "wire" the method calls together into the actual program
EliasGPS
EliasGPSOP3y ago
oh yeah ok thx
cap5lut
cap5lut3y ago
basically u could do something like
public class Program
{
public static void Main(string[] args)
{
var data = FetchData(); // here u can omit the Program.FetchData() and use just FetchData() because its within the same class
var result = ProcessData(data);
OutputResult(result);
}

private static string FetchData()
{
// use the HttpClient to grab data from an url
return "123"; // just an example
}

private static int ProcessData(string data)
{
return int.Parse(data);
}

private static void OutputResult(int result)
{
Console.WriteLine($"The result is {result}");
}
}
public class Program
{
public static void Main(string[] args)
{
var data = FetchData(); // here u can omit the Program.FetchData() and use just FetchData() because its within the same class
var result = ProcessData(data);
OutputResult(result);
}

private static string FetchData()
{
// use the HttpClient to grab data from an url
return "123"; // just an example
}

private static int ProcessData(string data)
{
return int.Parse(data);
}

private static void OutputResult(int result)
{
Console.WriteLine($"The result is {result}");
}
}
(these are ofc just rough examples) also i would recommend to do some beginner tutorials first to get the basics down (ofc, we r here to help on arising questions/problems) if ur question is answered now, please use /close to mark it as that @EliasGPS ⤴️
EliasGPS
EliasGPSOP3y ago
i just made another file with another class and than i make an object in the main file and call my method
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server