Question Trouble with Top-Level Statement

Riddler667

New member
Joined
Aug 16, 2022
Messages
2
Programming Experience
Beginner
Hello Im a beginner at C# coding and trying to make a simple cmd prompt that shows the status of a service running. Below is my code with the exception listed. Ive tried alot of different solutions with no avail. Please let me know if you can help out.
C#:
using System.ServiceProcess;
using System.Net;
using System.Data;
using System.Threading.Tasks;

static string GetWindowsServiceStatus(String SERVICENAME)
{

    ServiceController sc = new ServiceController(SERVICENAME);

    switch (sc.Status)
    {
        case ServiceControllerStatus.Running:
            return "Running";
        case ServiceControllerStatus.Stopped:
            return "Stopped";
        case ServiceControllerStatus.Paused:
            return "Paused";
        case ServiceControllerStatus.StopPending:
            return "Stopping";
        case ServiceControllerStatus.StartPending:
            return "Starting";
        default:
            return "Status Changing";
    }
}
namespace DNAChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("DNADrvr32 Status: " + GetWindowsServiceStatus("DNADrvr32"));             ----- CS8801    Cannot use local variable or local function 'GetWindowsServiceStatus' declared in a top-level statement in this context.
                    Console.ReadLine();
            }
            catch (Exception ex) { } 
        }
    }

1660687239099.png
 
Last edited by a moderator:
For future reference, please don't post unformatted code snippets. They are much harder to read, especially on mobile devices. Also, please provide meaningful thread titles. The title should be a summary of the problem so we have an idea of what the thread is about without opening it. "Beginner needs help" is as good as no title at all, because it describes 99% of all threads.
 
As for your issue, I'm afraid that you have butchered your project and should probably start again with a new one.

When you create a new project in VS 2022, one of the first decisions you need to make is whether you are going to target .NET Framework or .NET Core. I'm not going to go into the details of each but know that 4.8 was the last version of .NET Framework and that .NET 5 and later are based on .NET Core. When you create a project, all the templates that target .NET Framework say so in the name. If you select a project template without "(.NET Framework)" in the name then you're targeting .NET Core.

If you do target .NET Core then the latest version is .NET 6 (.NET 7 is available but only in preview). If you choose .NET 6 for a Console application then there is a box that indicates whether or not top-level statements will be used. Unchecked, which is the default, means that they will. Based on the error message, it appears that your project targets .NET 6 and uses top-level statements, but you have treated it like it doesn't.

If you're using top-level statements, the code file that you are presented with by default IS your Main method. The point of top-level statements is that you don't need a namespace declaration, a Program class declaration or a Main method declaration. You simply provide the body of the Main method and the compiler will handle the rest. C# supports local functions now, i.e. functions inside functions, so any function you declare in that default code file is a local function inside the Main method.

If you want to stick with this project, you need to get rid of that DNAChecker namespace, the Program class and the Main method. I'm guessing that you copied them from somewhere else but you have broken your project by doing so. What is inside the Main method should be what you put in the default code file, then you should have put the GetWindowsServiceStatus method after that. I don't think that there's any need or point to declaring that method static either, but I'm not 100% sure about that. You could read about local functions to find out, or just try it and see.

If you want to be able to use a full Main method then you can't use top-level statements. That means that you need to go back and create a new project and, if you do target .NET 6, check the box to not use them. The default code file will then already contain a namespace, class and method declaration.
 
As for your issue, I'm afraid that you have butchered your project and should probably start again with a new one.

When you create a new project in VS 2022, one of the first decisions you need to make is whether you are going to target .NET Framework or .NET Core. I'm not going to go into the details of each but know that 4.8 was the last version of .NET Framework and that .NET 5 and later are based on .NET Core. When you create a project, all the templates that target .NET Framework say so in the name. If you select a project template without "(.NET Framework)" in the name then you're targeting .NET Core.

If you do target .NET Core then the latest version is .NET 6 (.NET 7 is available but only in preview). If you choose .NET 6 for a Console application then there is a box that indicates whether or not top-level statements will be used. Unchecked, which is the default, means that they will. Based on the error message, it appears that your project targets .NET 6 and uses top-level statements, but you have treated it like it doesn't.

If you're using top-level statements, the code file that you are presented with by default IS your Main method. The point of top-level statements is that you don't need a namespace declaration, a Program class declaration or a Main method declaration. You simply provide the body of the Main method and the compiler will handle the rest. C# supports local functions now, i.e. functions inside functions, so any function you declare in that default code file is a local function inside the Main method.

If you want to stick with this project, you need to get rid of that DNAChecker namespace, the Program class and the Main method. I'm guessing that you copied them from somewhere else but you have broken your project by doing so. What is inside the Main method should be what you put in the default code file, then you should have put the GetWindowsServiceStatus method after that. I don't think that there's any need or point to declaring that method static either, but I'm not 100% sure about that. You could read about local functions to find out, or just try it and see.

If you want to be able to use a full Main method then you can't use top-level statements. That means that you need to go back and create a new project and, if you do target .NET 6, check the box to not use them. The default code file will then already contain a namespace, class and method declaration.
Thank you and sorry for the title. Is there any youtube channels or websites you use for checking your code or if you cant remember something?
 
It depends what sort of information you're after. For beginners, its a good idea to work your way through a good tutorial to learn the basics. Many tutorials will not have been updated for new features like top-level statements though, so you're not the first person to run afoul of that. It's important to keep your eyes open while doing things, which makes it more likely you'd notice that checkbox when creating a project. I believe that it has an information icon next to it that you can click to see what it actually means. Still, stuff like this is often only learned in just the way you have, which is why having sites like this available is a good thing.

With regards to code issues, my main advice is to always read the documentation. For any type or member, you can just click it in the code window and press F1 to go straight to the relevant topic, but I'd also recommend bookmarking the home page in your browser, so you can do quick searches. You can get there from the VS Help menu but that takes you to a higher-level page that requires a bit of navigation to get to the API reference. The Australian version is here, so you can probably just change the culture in the URL to find your local version. You won't always find what you want or understand what you find, particularly at first, but you'll get better as you use it more, plus you'll often learn things that will be useful later.
 
Also, side note on forum etiquette, you don't need to (shouldn't) click "Quote", quote an entire 500 words posting from another member and just say "Thanks" under it. Use the Quote function selectively on their content to give context to your own words, for example if someone wrote 500 words, then asked you 3 questions, use Quote on each of the 3 questions, and snip the 500 words that you are not responding to:

<snipped 500 words here>

What is your name?

John

How long have you been doing C#?

2 years


...
 
Back
Top Bottom