Marko Petričević
Marko Petričević

Reputation: 333

System.StackOverflowException while filling the list

Im gonna paste the code first so you can see what Im talking about:

namespace Radni_sati
{
public class Blagdani
{
    public List<Blagdani> Blagdani_lista = new List<Blagdani>();
    public DateTime datum { get; set; }
    public string dan_u_tjednu { get; set; }

    public Blagdani()
    {
        Blagdani KlasaBlagdan6 = new Blagdani();
        KlasaBlagdan6.datum = new DateTime(2017, 08, 05);
        KlasaBlagdan6.dan_u_tjednu = "Subota";
        Blagdani_lista.Add(KlasaBlagdan6);

        Blagdani KlasaBlagdan7 = new Blagdani();
        KlasaBlagdan7.datum = new DateTime(2017, 08, 15);
        KlasaBlagdan7.dan_u_tjednu = "Utorak";
        Blagdani_lista.Add(KlasaBlagdan7);

        //test blagdan
        Blagdani KlasaBlagdan8 = new Blagdani();
        KlasaBlagdan8.datum = new DateTime(2017, 09, 29);
        KlasaBlagdan8.dan_u_tjednu = "Petak";
        Blagdani_lista.Add(KlasaBlagdan8);

    } 
//some code afterwards

So my point here is to fill that list so I can use it later and Im not quite sure I understand whats happening in my case from what I have read on internet. Would apprisiate some explaning.

P.S. If someone can give me an example how to fill that list in the same class using those 2 properties (datum,dan_u_tjednu), that would be great.

Upvotes: 0

Views: 660

Answers (5)

Rufus L
Rufus L

Reputation: 37020

All you have to do is make both your List and constructor static. This way there is no recursive call in the constructor, since static refers to the type itself, not an instance of the type:

public class Blagdani
{
    public static List<Blagdani> Blagdani_lista;
    public DateTime datum { get; set; }
    public string dan_u_tjednu { get; set; }

    static Blagdani()
    {
        Blagdani_lista = new List<Blagdani>
        {
            new Blagdani {datum = new DateTime(2017, 08, 05), dan_u_tjednu = "Subota"},
            new Blagdani {datum = new DateTime(2017, 08, 15), dan_u_tjednu = "Utorak"},
            new Blagdani {datum = new DateTime(2017, 09, 29), dan_u_tjednu = "Petak"}
        };
    }
}

Now the list can be accessed without creating an instance of the type, like:

static void Main()
{
    foreach(var blagdani in Blagdani.Blagdani_lista)
    {
        Console.WriteLine($"{blagdani.datum} ({blagdani.dan_u_tjednu})");
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Output

enter image description here

Upvotes: 1

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

Try this, you are recursively calling your constructor, and every time you initilize the object it will go into endless loop, so you got overflow exception. By overriding constructor (or you can use a method) and converting List to static, you won't call your consturctor recursively, each constructor will do their own job, defining variable as static will remain your Blagdani, if it is not static every Blagdani property will be the last value.

public static List<Blagdani> Blagdani_lista = new List<Blagdani>();
public DateTime datum { get; set; }
public string dan_u_tjednu { get; set; }

public Blagdani() {
       DateTime date = new DateTime(2017, 08, 05);
       string something = "Subota";
       Blagdani b = new Blagdani(date, something);


       date = new DateTime(2017, 08, 15);
       something = "Utorak";
       b = new Blagdani(date, something);

       date = new DateTime(2017, 08, 29);
       something = "Petak";
       Blagdani qwe = new Blagdani(date, something);

}
public Blagdani(DateTime dt, string something) { // override constructor
       this.datum = dt;
       this.dan_u_tjednu = something;
       Blagdani_lista.Add(this);
}

Hope helps,

Upvotes: 1

Upender Singh
Upender Singh

Reputation: 39

Let me Explain what is happening in your case:
Below is your code:
1. Please focus on the comments section i.e which are in the form (// something ) 
   or is in the italic font

namespace Radni_sati { public class Blagdani { public List Blagdani_lista = new List(); public DateTime datum { get; set; } public string dan_u_tjednu { get; set; }

    public Blagdani()
    {
        **Blagdani KlasaBlagdan6 = new Blagdani();**    // *recursive call to the constructor again and again*
        KlasaBlagdan6.datum = new DateTime(2017, 08, 05);
        KlasaBlagdan6.dan_u_tjednu = "Subota";
        Blagdani_lista.Add(KlasaBlagdan6);

        Blagdani KlasaBlagdan7 = new Blagdani();
        KlasaBlagdan7.datum = new DateTime(2017, 08, 15);
        KlasaBlagdan7.dan_u_tjednu = "Utorak";
        Blagdani_lista.Add(KlasaBlagdan7);

        //test blagdan
        Blagdani KlasaBlagdan8 = new Blagdani();
        KlasaBlagdan8.datum = new DateTime(2017, 09, 29);
        KlasaBlagdan8.dan_u_tjednu = "Petak";
        Blagdani_lista.Add(KlasaBlagdan8);

    } 

Upvotes: 0

Kyle Burns
Kyle Burns

Reputation: 1214

This constructor is recursive. Since it creates new instances of the same class every time that it is invoked, you essentially have an infinite loop that will only terminate when the runtime runs out of memory or stack space to support it.

Upvotes: 0

TheSkimek
TheSkimek

Reputation: 342

Why not manage the list in another class? Remove the list from the Blagdani class and modify the constructor like this:

public Blagdani(string Xdan_u_tjednu_, DateTime Xdatum)
{
    dan_u_tjednu = Xdan_u_tjednu_;
    datum = Xdatum;
}

Then create a new class like:

public class ListManager
{
    public List<Blagdani> Blagdani_lista = new List<Blagdani>();

}

Now you can just add to this List by:

Blagdani_lista.Add(new Blagdani("Subota",new DateTime(2017, 08, 05));
Blagdani_lista.Add(new Blagdani("Utorak",new DateTime(2017, 08, 15));

Upvotes: 1

Related Questions