C
C#2y 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
EliasGPS2y 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
cap5lut2y ago
then use regular classes instead of top-level statements in the other files
EliasGPS
EliasGPS2y ago
uhh what? i'm new to c# soory
cap5lut
cap5lut2y 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
EliasGPS2y ago
oh ok how can i use regular classes than?
SwaggerLife
SwaggerLife2y ago
C# only allows top level in Program.cs.
cap5lut
cap5lut2y 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
SwaggerLife
SwaggerLife2y ago
You have to put your code inside classes.
EliasGPS
EliasGPS2y ago
yeah yeah i understand is there an externsion is vscode to beautify my code
SwaggerLife
SwaggerLife2y ago
You can reformat using shit alt f You should be good to go if you have the c# extension installed.
cap5lut
cap5lut2y 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
EliasGPS2y ago
oh yeah ok thx
cap5lut
cap5lut2y 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
EliasGPS2y ago
i just made another file with another class and than i make an object in the main file and call my method
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts