【问题标题】:Compile time error - member cannot be accessed with an instance reference编译时错误 - 无法使用实例引用访问成员
【发布时间】:2015-12-11 00:07:16
【问题描述】:

我是一个试图理解类以及类如何工作的新手。我正在构建一个小型控制台程序,目前正在处理我的名为“LineItem.cs”的“class.cs”文件,因为它将处理我试图让我的控制台应用程序生成的收据上的行项目。

问题:无法使用实例引用访问成员“A070__CashRegister.Program.receipt()”;而是用类型名称来限定它。 (错误行:#21/第 13 列)

当我输入 'this.color = price;

时,我以为我在第 21 行完成了这个

代码:

using System;
namespace a070___Classes___CashRegister
{
  class LineItem     // Class name is singular
                     // receipt might be better name for this class?
  {
    /// Class Attributes
    private String product;     // constructor attribute
    private String description;
    private String color;
    private double price;
    private Boolean isAvailable;

    // constructor called to make object => LineItem
    public LineItem(String product, String description, String color, int price, Boolean isAvailable)
    {
        this.product = product;
        this.description = description;
        this.color = color;
        this.price = price;
        this.isAvailable = isAvailable;// might want to do an availability check
    }

    //Getters
    public String GetProduct() {return product;}
    public String GetDescription(){return description;}//Send description
    public String GetColor() {return color;}

    //Setter
    public void SetColor(string color)//we might want to see it in other colors if is option
    { this.color = color; } //changes object color 
  }
}

调用类的主文件:

using System;

namespace a070___Classes___CashRegister
{
  class Program
  {
    static void receipt()
    { 
    //stuff goes here - we call various instances of the class to generate some receipts
    }

    static void Main(string[] args)
    {
        //Program CashRegister = new Program();
        //CashRegister.receipt();

        //Program CashRegister = new Program();
        //CashRegister.receipt();

        receipt();// Don't need to instantiate Program, console applications framework will find the static function Main
        //unless changed your project properties.
        //Since reciept is member od Program and static too, you can just call it directly, without qualification.
    }
  }
} 

【问题讨论】:

  • 代码不显示reciept,既不使用也不声明。
  • 请务必阅读 MSDN 上的错误说明:CS0176
  • 旁注:请避免在帖子中使用长的“new here”/“searched alot”/“thankyou notes”文本。而是考虑添加与您的错误相关的示例代码。
  • 我阅读了 MSDN: CS0176,但不理解它或它与我的问题有何关系。我发布了代码,这是另一个将调用该类的文件。我正在尝试首先构建我的类文件,我的错误都发生在我的类文件中(总共是一个错误)。

标签: c# class instance


【解决方案1】:
Program CashRegister = new Program();
CashRegister.receipt();

应该是

Program.receipt();

或者只是

receipt();

您不需要实例化 Program,使用控制台应用程序,框架会找到 static function Main(... 并通过魔法调用它,除非您更改了项目属性。

由于receiptProgramstatic的成员,你可以直接调用它,无需限定。


receipt() 函数是 static,但您正在尝试从实例调用它。

您没有显示receipt 的声明位置或调用它的位置,所以我无法提供更多帮助。

也许你有一行代码,其中某处有一个表达式,

... this.receipt() ...

... yourInstance.receipt() ...

但应该是,

... Type.receipt() ...

【讨论】:

  • 感谢您的一般解释,非常感谢。我假设当您说“receipt()”时,您指的是“Class LineItem”,是吗?我将如何解决这个问题?我尝试将第 16 行的“int price”更改为“double price”和“double price”,但这些只会增加错误。其他“this”元素都不会产生错误。我很困扰。再次感谢您的帮助
  • @Chezshire,我指的是您的异常消息中明确引用的reciept()。因此,我可以知道,这不是正确的代码。
  • 我修改了我的主文件以反映你建议的“reciept()”语句,这清除了一切。我现在正在谷歌上搜索一些人们在这个线程中提到的东西,以增加我的理解。谢谢您的帮助。我还更新了我的主代码文件。
  • @Jodrell - 在您的回答中,您以 2 种不同的方式拼写“receipt”。您应该将它们拼写相同,最好是正确的方式:“recEIpt”,而不是“recIEpt”。
  • @DavideAndrea,我认为已修复
【解决方案2】:

您不能通过实例访问静态方法。

您需要做的就是使用这样的类来访问它:

LineItem.receipt();

注意:你没有提到其他代码,所以我不知道方法接收的位置,所以我假设它在 LineItem 类中。

还有一件事,最好用大写字母调用方法 - 收据。

【讨论】:

  • 谢谢你 Misha,你的 cmets 非常有帮助和感激
  • 感谢 (R)eceipt Misha 的关注。每一点都有帮助,每一点都值得赞赏。
猜你喜欢
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多