排除故障的最佳方法“命名空间中不存在名称...”错误 [英] Best way to troubleshoot "The name ... does not exist in the namespace" errors

查看:89
本文介绍了排除故障的最佳方法“命名空间中不存在名称...”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习WPF / EF / MVVM的方法,并且正在取得一些进展,但是Visual Studio时不时声称名称无论在clr-namespace中不存在HomeOfWhatever,当它清楚的时候确实如此!指出那个,甚至在屏幕上大喊大叫似乎没有帮助。也没有清理和重建,也没有重新启动VS.



谷歌搜索表明这个错误可能是由各种各样的事情造成的,大多数事情(对于我的新手来说)似乎与有问题的名称或命名空间没什么关系。



而不是随机粘贴问题(尝试Googling建议的所有各种修复),似乎应该有一种方法上的方法将问题域缩小到可管理的范围。(有意义的错误消息在这里将是一个很大的帮助,微软!)(或者错误消息可能有意义,我只是不知道还有解释它。)



我希望如此在那里的人可以解释那个消息试图说什么,可能违反了什么样的规则,甚至可能提供一个全面的要检查的清单。例如:不要试图从网络共享中做到这一点,你的类必须有一个公共的,无参数的构造函数。 它不能在另一个班级内,如果它与你班级的装配不同,你必须专门参考装配等等。



谢谢。

解决方案

仅仅因为名称空间中存在名称,并不代表你可以使用它!



一般来说(当然对初学者而言)遵循简单的规则:一个项目,一个名称空间。如果您需要第二个命名空间,请创建第二个项目(创建一个类库)并将其引用添加到原始项目中,并在代码文件中使用使用语句这将使用新项目包含的类。

 MyEXEProject MyClassLibraryProject 
MyEXEClass.cs MyClassLibrary.cs
使用系统; 使用系统;
...... ...
使用 MyClassLibrary

namespace MyEXEProject 命名空间 MyClassLibrary
{{
public partial class MyForm:Form public 部分 MyClass
{{
MyClass myClass = new MyClass();
...... ...
}}
}}





一旦你有了,你应该可以使用任一命名空间中的类,就像它们在同一个(几乎)一样。



你有什么不同?


I'm struggling to learn my way around WPF/EF/MVVM and am making some progress, but every now and then Visual Studio claims that "The name "Whatever" does not exist in the clr-namespace "HomeOfWhatever", when clearly, it does! Pointing that out, or even yelling at the screen doesn't seem to help. Nor does cleaning & rebuilding, nor does re-starting VS.

Googling indicates that this error can be caused by a wide variety of things, most of which seem (to my novice's eye) to have little to do with the name or namespace in question.

Rather than random stick-poking at the problem (trying all the assorted "fixes" suggested by Googling), it seems that there ought to be a methodical way of narrowing the problem domain to something manageable. (Meaningful error messages would be a big help here, Microsoft!) (Or maybe the error message IS meaningful, and I just don't know enough yet to interpret it.)

I'm hoping that someone out there can explain what that message is trying to say, what kind of rules might have been violated, and maybe even provide a comprehensive checklist of things to look for. Such as: "Don't try to do this from a network share", "Your class has to have a public, parameterless constructor." "It can't be inside another class", "You have to specifically reference the assembly if it is different from your class's assembly", etc.

Thanks.

解决方案

Just because a name exists in a namespace, doesn't mean you can just use it!

Generally speaking (and certainly for beginners) follow the simple rule: one project, one namespace. If you need a second namespace, create a second project (that creates a Class Library) and add a reference to that to the original project together with a using statement in the code files that will use the classes the new project contains.

MyEXEProject                                   MyClassLibraryProject
MyEXEClass.cs                                  MyClassLibrary.cs
using System;                                  using System;
...                                            ...
using MyClassLibrary                           

namespace MyEXEProject                         namespace MyClassLibrary
   {                                              {
   public partial class MyForm: Form              public partial class MyClass
      {                                              {
      MyClass myClass = new MyClass();               
      ...                                            ...
      }                                              }
   }                                              }



Once you have that, you should be able to use the classes from either namespace as if they were in the same one (almost).

What do you have that is different?


这篇关于排除故障的最佳方法“命名空间中不存在名称...”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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