Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';字段';但是像';类型';_C#_.net - Fatal编程技术网

C# ';字段';但是像';类型';

C# ';字段';但是像';类型';,c#,.net,C#,.net,我收到了一条错误信息,我似乎无法理解。我看到有许多其他类似的问题张贴,但解决方案似乎不适合我 错误消息: 'PaymentPlanStoreLogic.Class1.myLog' is a 'field' but is used like a 'type' 我的代码: using System; using PaymentPlanLogic; namespace PaymentPlanStoreLogic { public class Class1 { Logger myLog

我收到了一条错误信息,我似乎无法理解。我看到有许多其他类似的问题张贴,但解决方案似乎不适合我

错误消息:

'PaymentPlanStoreLogic.Class1.myLog' is a 'field' but is used like a 'type'
我的代码:

using System;
using PaymentPlanLogic;
namespace PaymentPlanStoreLogic
{
  public class Class1
  {
    Logger myLog = new Logger();
    myLog.createLog();
   }
}
对象浏览器:

public void createLog()
Member of PaymentPlanLogic.Logger
我已经按照页面上的建议尝试了以下方法

但是我得到了错误

"'PaymentPlanLogic.Logger.createLog()' is a 'method' but is used like a 'type'"

不能在方法之外的字段上调用方法:

using System;
using PaymentPlanLogic;
namespace PaymentPlanStoreLogic
{
  public class Class1
  {
    Logger myLog = new Logger();

    void YouForgotThisMethod()
    {
      myLog.createLog();
    }
  }
}

Class1
的定义不完整。不能在类内调用此方法,可以在类的方法内调用它:

using System;
using PaymentPlanLogic;
namespace PaymentPlanStoreLogic
{
    public class Class1
    {
        #region Members

        Logger myLog;

        #endregion

        #region Constructors

        public Class1()
        {
            myLog = new Logger();
            myLog.createLog();
        }

        #endregion
    }
}

然后,
createLog()
将在实例化Class1的新实例时执行。

Tobsey的答案是正确的。出现奇怪错误消息的原因是,编译器正拼命想弄明白您的意思。理由如下:

此时唯一合法的是类、接口、结构、枚举、字段、运算符、索引器、事件、属性、方法、构造函数和析构函数的声明。它不能是类、接口、结构或枚举,因为它们都有关键字。它不能是构造函数或析构函数。因此,用户必须尝试创建字段、运算符、索引器、事件、属性或方法。所有这些事情都可以合法地从一个类型开始。好的,所以这个表达式必须尝试标识一个类型。但它不能识别一种类型;它标识一个字段。这就是我要说的错误

当然,这是对编译器的一种不合理的拟人化,但您要抓住要点:编译器错误代码的“心理模型”与您编写错误代码时的想法非常不同,因此错误消息没有多大帮助。编译器认为您知道语句不能到达那里,并且将字段与类型混淆了。更复杂的算法会让编译器猜测,因为您键入的是合法语句,所以真正的错误是该语句在块之外


编写错误消息生成器来准确地模拟编写错误代码的人的心理过程是相当困难的。

您不应该在构造函数的Class1中包含该代码吗?第一行将在类中创建一个私有成员,但第二行没有任何意义,因此您会遇到此错误。如果这是您的完整代码,我建议查看一些。YouForgott此方法可能是缺少的构造函数(我认为)。@ken2k是的,您可能是对的,不必在构造函数中,所以我给出了一般的答案谢谢,我知道我在做一些愚蠢的事情。
using System;
using PaymentPlanLogic;
namespace PaymentPlanStoreLogic
{
    public class Class1
    {
        #region Members

        Logger myLog;

        #endregion

        #region Constructors

        public Class1()
        {
            myLog = new Logger();
            myLog.createLog();
        }

        #endregion
    }
}