-1

I'm having trouble creating static object list... I've created

public class KrepselisClass
{
    public static List<KrepselisClass> KrepselisList = new List<KrepselisClass>();
}

and declared the list inside this class. But how do i access or put things inside the List. (I'm creating a restaurant menu and right now I'm trying to make a shopping cart)

adjan
  • 13,371
  • 2
  • 31
  • 48
  • 1
    This simply means you don't know what static means, and try not using it until and unless absolutely necessary, its a convenience that can lead to lot of problems due to shared state, which can be easily corrupted – Mrinal Kamboj Oct 14 '18 at 10:29
  • 1
    I've tried looking for public list that I could use in whole program and the static list would come out in every post... – PetriuxD Nezinau Oct 14 '18 at 10:31
  • The thing I'm trying to accomplish is getting object information from one class to another – PetriuxD Nezinau Oct 14 '18 at 10:32
  • Why do you want a class that has a static list referencing to itself? It seems like poor design decision to me – fasaas Oct 14 '18 at 10:37
  • 2
    use it as KrepselisClass.KrepselisList – Joseph Wu Oct 14 '18 at 10:40
  • I would think that for a restaurant menu, you'd want to populate it from a data source such as a database. – Powerlord Oct 14 '18 at 10:57
  • According to your code you have created the list successfully. I guess you have difficulties to access it. Please edit your post and correct it. – Mong Zhu Oct 14 '18 at 17:19

3 Answers3

0

It is really hard to understand (and guess) what you want to achieve by creating a list of a type as a static member inside the same type, but the answer for your question on how to create and access it is:

public class Program
{
    public static void Main()
    {
        var myNewInstance = new KrepselisClass();
        KrepselisClass.KrepselisList.Add(myNewInstance);

        // OR

        KrepselisClass.KrepselisList.Add(new KrepselisClass());
    }
}

public class KrepselisClass
{
    public static List<KrepselisClass> KrepselisList = new List<KrepselisClass>();
}

Please take a look here for the code sample: https://dotnetfiddle.net/Gwa7aI

But before creating a new system with static classes and members, I'd suggest to take a look on this answer, I really like it: https://stackoverflow.com/a/241372/4537762

Anderson Rancan
  • 366
  • 5
  • 13
0

I do not understand why do you want to create a static list inside this class, but I am sure there should be an important reason.

First of all, you should know when to use static keyword. When you declare something with static keyword inside a class, you can access them directly from that particular class without creating objects.

public class Program
{
    public static void Main()
    {      
         KrepselisClass.KrepselisList.Add(new KrepselisClass());
    }
}

public class KrepselisClass
{
    public static List<KrepselisClass> KrepselisList = new List<KrepselisClass>();
}

As you can see above in my main method I have directly used the list without creating an object of KrepselisClass.

Since I do not understand what you are trying to do, this is all I tell. This should solve most of your problem

Please refer these links to get a clear idea.

https://theburningmonk.com/2010/07/static-vs-non-static-method-in-csharp/

https://softwareengineering.stackexchange.com/questions/163457/understanding-the-static-keyword

https://www.sitesbay.com/java/java-static-keyword

  • Check these out https://theburningmonk.com/2010/07/static-vs-non-static-method-in-csharp/ – Gihan Saranga Siriwardhana Oct 14 '18 at 17:17
  • Your answer is misleading. You do create the object / list. It is done inside the class where you call the lists constructor. You should clarify that. Up to now it is simply wrong that you can use a static object without creating it. – Mong Zhu Oct 14 '18 at 18:01
  • What I meant was you can call static objects inside a class, without creating an object of that particular class. Not the List or object. It is obvious that you have to create an object of List by calling the constructor of List – Gihan Saranga Siriwardhana Oct 15 '18 at 01:13
  • It should be obvious in your post and not ambiguous. It is not obvious if one has not enough experience with programming. You should clarify that. – Mong Zhu Oct 15 '18 at 09:01
0

I'm trying to accomplish shopping cart type thing. For that reason I need list that I could use through out the whole program because I'm going to show menu of items in one window and shopping cart in another.

p.s. I've googled public list and I got static list as result I'm not trying to use it because I don't understand it.