Please help me with my final project in my first programming class.. CS0120

alloca

New member
Joined
May 3, 2016
Messages
3
Programming Experience
Beginner
So below is my code (really part of my code but at the moment all other methods and stuff is commented out and i want to try this piece of code)... my problem is i get the error message

"Error CS0120 An object reference is required for the non-static field, method, or property 'Sodacrate.Run()"

it just doesn?t go to "Sodacrate.Run()" what am i doing wrong?

the swedish in the code is just menu selecting.



Here it is!
using System;

namespace sodacrate
{

//create a class for sodas and declairing what?s required for a soda.

    class Soda
    {
        private string brand;
        private double price;
        private string type;

        public Soda(string _brand, double _price, string _type) //constructor
        {
            brand = _brand;
            price = _price;
            type = _type;
        }
    }

//Creates a class for sodacrate wich includes an array of 24 bottles

    class Sodacrate
    {

        private Soda[] bottles = new Soda[24];
           
              public void Run()
        {


            Console.WriteLine("V?lkommen till din digitala l?skbacks organiserare!");
            Console.WriteLine("Just nu inneh?ller din back 0 flaskor");
            Console.WriteLine("F?r att l?gga till en l?sk i backen tryck (1)");
            Console.WriteLine("F?r att se vad som finns i backen tryck (2)");
            Console.WriteLine("F?r att se totalpriset f?r din nuvarande back tryck (3)");
            Console.WriteLine("F?r att avsluta programmet tryck (4)");
            Console.ReadLine();
            int menyVal = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("TEST");
  }

        public void add_soda()
        {
            Console.Write("Vad heter l?sken du vill l?gga till?:");
            string brand = Console.ReadLine();
            Console.Write("Vad kostar den?:");
            double price = Convert.ToDouble(Console.ReadLine());

            Console.Write("ange typ (tex l?tt?l/l?sk/vatten:");
            string type = Console.ReadLine();
            Soda soda = new Soda(brand, price, type);
            Console.WriteLine(soda);
        }

 }
        class Program
        {
            public static void Main(string[] args)
            {
                //Skapar ett objekt av klassen Sodacrate som heter sodacrate
                var sodacrate = new Sodacrate();
                Sodacrate.Run();
                Console.Write("Press any key to continue . . . ");
                Console.ReadLine();
                Console.ReadKey(true);
            }
        }
    }




Please help me :(
 
Last edited by a moderator:
Here's the offending code:
var sodacrate = new Sodacrate();
Sodacrate.Run();
C# is case-sensitive so 'sodacrate' and 'Sodacrate' are not the same thing. You are calling Run on the type rather than on the variable of that type that you just declared. This is a perfect example of why naming variables after there type is a bad idea.
 
Back
Top Bottom