-1

First i'll give you a run around my code, I have a class that stores some data :

public class Balta
    {
        public int x;
        public int y;
        public int raza;
        public int cantApa;
        public int id;
        public int intersect;
        public Balta(int xs, int ys, int r, int cApa, int ids, int intersctt)
        {
            x = xs;
            y = ys;
            raza = r;
            cantApa = cApa;
            id = ids;
            intersect = intersctt;
        }        
    }

Secondly I have a class that makes a list of that data and stores it acordingly,also i intend to do some operations with that data once i get rid of this pesky error.

public class Baltile
    {
        public int n;
        List<Balta> balti = new List<Balta>();

        public void populate(Balta balta)
        {
            int unitId = balta.id;
        if (balti.Any(Balta => Balta.id == balta.id))
        {
            int pos = balti.FindIndex(Balta => Balta.id == balta.id);
            balti[pos] = balta;
        }
        else if (balti.Any(Balta => Balta.cantApa == -1) && !balti.Any(Balta => Balta.id == unitId)) 
        {                    
                int pos = balti.FindIndex(Balta => Balta.cantApa == -1);
                balti[pos] = balta;                                                             
        }
        else //daca nu inseamna ca aduaugi balta la lista
        {
            balti.Add(balta);
        }
        }
    }

And main looks something like this

 static void Main(string[] args)
        {             
            Baltile balti = new Baltile();
            while (true)
            {
              "data input block"
              for (int i = 0; i < unitCount; i++)
              {
               "more data input"    
               if (i>2)
               {
                Balta balta = new Balta(x, y, radius, extra, unitId, 0);
                Baltile.populate(balta);//the CS0120 error is here
               }
             }
           }
         }

So CS0120 tells me this An object reference is required for the non-static field, method, or property.This means main is static so i cant call a non static method if i understood right ? Declaring everything static will give even more errors.

How should i go around this ? I cant seem to figure out how to make my code work ?

BushLee
  • 65
  • 9

1 Answers1

1

With

Baltile.populate(balta);

and Baltile being a class name, this would require populate() to be a static method.

As per MSDN, the full error message is

Compiler Error CS0120

An object reference is required for the nonstatic field, method, or property 'member'

This tells you to use an object instead of a class. And it seems you already have an object called balti that serves this purpose. So use

balti.populate(balta);

instead. Now you call the populate() method on an instance (an object) instead of the class.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222