编译时间错误 - 无法使用实例引用访问成员 [英] Compile time error - member cannot be accessed with an instance reference

查看:190
本文介绍了编译时间错误 - 无法使用实例引用访问成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的试图理解类和类如何工作。我正在建立一个小控制台程序,目前正在我的'class.cs'文件,我称之为'LineItem.cs',因为它将处理收据上的我想让我的控制台应用程序生成的行项目。 / p>


PROBLEM:无法使用实例引用访问成员'A070_ _CashRegister.Program.receipt使用类型名称来限定它。 (错误行:#21 /列#13)


我以为我在第21行做了这个.color = price;



代码:

  
命名空间a070___Classes___CashRegister
{
类LineItem //类名是单数
//收据可能更好的名称为这个类?
{
///类属性
private String产品; // constructor attribute
private String description;
private String color;
private double price;
private Boolean isAvailable;

//构造函数调用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; //可能要进行可用性检查
}

// Getters
public String GetProduct(){return product;}
public String GetDescription(){return description;} //发送描述
public String GetColor(){return color;}

// Setter
public void SetColor字符串颜色)//我们可能想看到它在其他颜色如果是选项
{this.color = color; } //更改对象颜色
}
}

类:

 使用System; 

命名空间a070___Classes___CashRegister
{
类程序
{
static void receipt()
{
//这里的东西 - 我们调用类的各种实例来生成一些收据
}

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

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

receipt(); //不需要实例化程序,控制台应用程序框架将找到静态函数Main
//除非更改了项目属性。
//由于reciept是成员od程序和静态,你可以直接调用它,没有资格。
}
}
}


解决方案>

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

应为

  Program.receipt(); 

或只是

 code> receipt(); 

您不需要实例化 Program ,使用控制台应用程序框架将找到静态函数Main(... 并通过魔法调用,除非你已经更改了项目属性。



由于收件 Program static的成员






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


$ b b

您不是在显示收据被宣布或您正在呼叫它,因此我不能帮助更多。



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

  ... this.receipt()... 

  ... yourInstance.receipt()... 

但应为

  ... Type.receipt()... 


I'm a new trying to understand classes and how classes work. I'm building a small console program and am currently working on my 'class.cs' file which I titled 'LineItem.cs' as it will handle the line items on the receipt that I am trying to have my console application generate.

PROBLEM: Member 'A070_Classes_CashRegister.Program.receipt()' cannot be accessed with an instance reference; qualify it with a type name instead. (Error Line: #21/Column #13)

I thought that i had done this on line #21 when I entered 'this.color = price;

Code:

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

Main file which will call the class:

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

解决方案

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

should be

Program.receipt();

or just

receipt();

You don't need to instantiate Program, with console applications the framework will find the static function Main(... and call that by magic, unless you've changed your project properties.

Since receipt is a member of Program and static too, you can just call it directly, without qualification.


The receipt() function is static but you are trying the call it from an instance.

You are not showing either where receipt is declared or where you are calling it from so I can't help more.

Perhaps you have a line of code, somewhere in it there is an expression like,

... this.receipt() ...

or

... yourInstance.receipt() ...

but should be,

... Type.receipt() ...

这篇关于编译时间错误 - 无法使用实例引用访问成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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