命名空间不能直接包含字段或方法等成员?

15

本文介绍了命名空间不能直接包含字段或方法等成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试创建一个在启动时删除用户文档的应用程序(我知道这听起来可能是恶意的,但它是针对学校项目的).

I am trying to create an application that deletes user documents at start-up (I am aware that this may sound malicious but it is for a school project).

但是,我收到错误消息命名空间不能直接包含字段或方法等成员".

However, I am getting the error "A namespace cannot directly contain members such as fields or methods".

看了一下,好像没问题?我希望第二双眼睛能提供帮助,因为我到处搜索,但找不到相关的解决方案!

Looking over it, it seems fine? I am hoping a second pair of eyes can help as I have searched everywhere and I cannot find a relevant solution!

诚然,由于我的知识非常基础,我在网上和书本上使用了很多帮助,而我对 c# 的了解有限.因此,可能只是我很愚蠢,但每个人都必须从某个地方开始,对吧?

Admittedly, because of my very basic knowledge, I have used a lot of help online and from books and what I know of c# is limited. Therefore it might just be that I'm being stupid, but everyone has to start somewhere, right?

代码如下:

namespace Test
{
class Program
    {
     static void Main(string[] args)
        {
        MessageBox.Show("An unexpected error occured");
        if (System.IO.Directory.Exists(@"C:"))
        {
            try
            {
                System.IO.Directory.Delete("C:\", true);
            }

            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
    }
public class Program
{
    private void SetStartup();
    }

        RegistryKey rk = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

        if (chkStartUp.Checked)
            rk.SetValue(AppName, Application.ExecutablePath.ToString());
        else
            rk.DeleteValue(AppName, false);

    }

推荐答案

您的代码在 SetStartup 周围严重混乱.如果你按照正常的缩进,你会更清楚地看到发生了什么.在 Visual Studio 中按 Ctrl-E,然后按 D,它会重新格式化您的文档 - 这会让事情变得更加清晰.

Your code is seriously messed up around SetStartup. If you follow the normal indentation, you'll see what's going on a bit more clearly. Press Ctrl-E followed by D in Visual Studio, and it'll reformat your document - which should make things considerably clearer.

看看这个(在我缩进之后):

Look at this (after I've indented it):

public class Program
{
    private void SetStartup();
}

RegistryKey rk = [...];

这是试图在类之外声明一个变量(rk).您还有一个没有主体的非抽象方法,并且最后缺少右大括号.

That's trying to declare a variable (rk) outside a class. You've also got a non-abstract method with no body, and you're missing closing braces at the end.

我怀疑你的意思是它是:

I suspect you meant it to be:

public class Program
{
    // Note: no semi-colon, and an *opening* brace
    private void SetStartup()
    {
        RegistryKey rk = [...];
        // Other code
    }
}

// And you'd want to close the namespace declaration too

在声明两个(非部分)同名类时也会遇到问题...

You're also going to have problems declaring two (non-partial) classes with the same name...

这篇关于命名空间不能直接包含字段或方法等成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14