Benjamin
Benjamin

Reputation: 77

Object reference required for non static fields

I can't seem to figure out what's wrong.

if I remove the static modifier then i have a problem when i call items.getTotal() if i add it back into the method then i get a problem returning the number field.

Any help with this would be appreciated I'm still learning.

I've marked below beside with comments which line i have error in.

namespace Something
{
class Program
{
    static void Main()
    {
        List<items> item = new List<items>();
        int choice;
        while (true)
        {
            Console.WriteLine("Please select an option:");
            Console.WriteLine("1) Create item:");
            Console.WriteLine("2) Destroy last item:");
            Console.WriteLine("3) Exit:");
            choice = int.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    Console.Write("Name of new item:");
                    items obj = new items(Console.ReadLine());
                    item.Add(obj);
                    break;
                case 2:
                    if (item.Count > 0)
                    {
                        item[new item.Count - 1].RemoveOne(); /*<<error*/
                        item.RemoveAt(item.Count - 1);
                    }
                    break;
                case 3:

                    Console.WriteLine("Total number of items:" + items.getTotal()); /*<<error*/
                    Environment.Exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}
class items
{
    //write your code here

    private string name;
    private int number;



    public items(string a)
    {
        this.name = a;
        this.number++;
        Console.WriteLine("successfully created");
    }
    public void RemoveOne()
    {
        Console.WriteLine(this.name);
        this.number = this.number + 1;   
    }

    public int getTotal()
    { 
        return number;
    }



}

} 

Upvotes: 0

Views: 90

Answers (3)

Eric J.
Eric J.

Reputation: 150148

Some of your fields, properties and methods are instance scoped, and some are static. You need to be consistent in order for them to access each other.

Something that is static means that it is associated with the class itself. When something is not static, it is associated with a specific instance of the class.

Given what you seem to be working on, I suggest you remove static from getTotal():

public int getTotal()
{ 
    return number;
}

If I remove the static modifier then i have a problem when i call items.getTotal() if i add it back into the method then i get a problem returning the number field.

I'm not sure which "static modifier" you are referring to. If you remove it from getTotal(), I don't see anything that would obviously break. If something does, update your question with the specific line that breaks.

UPDATE

The line

item[new item.Count - 1].RemoveOne(); /*<<error*/

should not have the new keyword in it. Simply item.Count - 1 will give you the index of the last element in item.

The line

items.getTotal()

is giving you problems because items is the name of a class, not a variable. That syntax will try to call a static method on the class called getTotal(). You probably mean

item.getTotal()

which refers to your instantiated List<items>.

See also the following note about casing. It will make it easier to avoid this type of issue.

About Style

I suggest you follow the C# coding conventions, that specify that classes and methods shall be CamelCase, e.g.

class Items
{
    public int GetTotal()
    { 
        return number;
    }
}

Following that convention makes your code much easier for others to read, and for you as well.

Upvotes: 1

DevEstacion
DevEstacion

Reputation: 1977

The problem is the static method getTotal() gets the default value of private int number;.

What you must do is either make the number field static or make the method getTotal() non-static.

You must create an instance of the item class and then from the instance, you can call the getTotal() method.

Example

item myItem = new item();
myItem.getTotal();

Upvotes: 0

TGH
TGH

Reputation: 39268

    public static int getTotal()
    { 
        return number;
    }

You can't reference number inside getTotal since number is an instance member and getTotal is static. Static members are defined at the type level, so there is no knowledge of specific instance based members, so it's invalid to make a reference to an instance member from a static type.

Upvotes: 0

Related Questions