我正在尝试在我的类中添加一个字段并在其他命名空间中使用它。我正在修改现有的 c# 项目,因此该类已经具有在其他命名空间中使用的字段。
这是我的类的代码,它写在我的名为“ModeleDeDomaine”的命名空间中:
namespace ModeleDeDomaine
{
public class AlgorithmeGestionDesDouelles
{
public bool enCours;
public bool Pasedff;
}
}
第一个字段“enCours”在我开始处理此代码时已经创建。第二个是我想添加的。这是我的命名空间“IHM”中的代码摘录:
namespace IHM
{
public MainWindow() {
if (AlgorithmeGestionDesDouelles.enCours) {
...
}
}
}
编译时我没有错误,语法似乎是正确的。但是如果我用另一个字段写同样的东西:
namespace IHM
{
public MainWindow() {
if (AlgorithmeGestionDesDouelles.Pasedff) {
...
}
}
}
我有以下语法错误:Error CS1061: 'AlgorithmeGestionDesDouelles' does not contain a definition for 'Pasedff' and no extension method 'Pasedff' accepting a first argument of type 'AlgorithmeGestionDesDouelles' could be found (are you missing a using directive or an assembly reference?
为什么我不能在这个命名空间中使用我的字段?
预先感谢您的所有回答,
柯林
最佳答案
AlgorithmeGestionDesDouelles.enCours
的用法表明这是一个静态字段,可以在没有实例的情况下引用(调用)。但是,这与您提供的代码示例不同:它是一个实例字段。
如果您更改添加到 public static bool Pasedff;
的字段, 您的 main
中的代码应该编译。我不知道这是否是你想要的东西。
以下是一些可能有用的静态类信息:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members
关于c# - 不能在不同的命名空间中使用我的新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64463944/