1

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.
    }
  }
} 
Chezshire
  • 713
  • 5
  • 13
  • 32
  • 2
    The code does not show `reciept`, neither use or declaration. – Jodrell Nov 08 '13 at 17:18
  • Please make sure to read error explanation on MSDN: [CS0176](http://msdn.microsoft.com/en-us/library/zhcxt2bd%28v=vs.90%29.aspx) – Alexei Levenkov Nov 08 '13 at 17:22
  • Side note: please avoid long "new here"/"searched alot"/"thankyou notes" text in the post. Instead consider adding sample code that relates to your error. – Alexei Levenkov Nov 08 '13 at 17:23
  • I read MSDN: CS0176, but did not understand it or how it relates to my issue unfortunately. I posted the code up which is the other file that will call the class. I'm trying to build my class file first, my errors all happen in my class file (which is one error total). – Chezshire Nov 08 '13 at 17:34

2 Answers2

5
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() ...
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Thank you for the general explanation, that is appreciated. I'm assuming that when you say 'receipt()', you are referring to the 'Class LineItem' yes? How would i fix this? i've tried changing 'int price' on line #16 to 'double price' and 'Double price' but those only added errors. none of the other 'this' elements produce an error. I am very confused. Again, thank you for the help – Chezshire Nov 08 '13 at 17:21
  • @Chezshire, I'm referring to the `reciept()` that is explicity cited in your exception message. Hence, I can know, this is not the right code. – Jodrell Nov 08 '13 at 17:23
  • I revised my main file to reflect the 'reciept()' statement as you suggested and that cleared up everything. I am now googling a bit of stuff on the things folks have mentioned in this thread to work on increasing my understanding. Thank you for your help. I also updated my main code file. – Chezshire Nov 08 '13 at 17:39
  • @Jodrell - In your answer you spell "receipt" in 2 different ways. You should spell them all the same, preferably the correct way: "recEIpt", not "recIEpt". – Davide Andrea Dec 10 '15 at 15:46
  • 1
    @DavideAndrea, fixed, I think – Jodrell Dec 10 '15 at 16:08
0

You can't access a static method with an instance.

All you need to do is to access it with the class like this:

LineItem.receipt();

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.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116