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.
}
}
}
should be
or just
You don't need to instantiate
Program
, with console applications the framework will find thestatic function Main(...
and call that by magic, unless you've changed your project properties.Since
receipt
is a member ofProgram
andstatic
too, you can just call it directly, without qualification.The
receipt()
function isstatic
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,
or
but should be,
You can't access a static method with an instance.
All you need to do is to access it with the class like this:
Note: You haven't mention the other code so I don't know where the method receipt locates so I have assumed that it is in the LineItem class.
And one more thing, it is better to call methods with a capital letter - Receipt.