0

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.

Marko Petričević
  • 333
  • 2
  • 9
  • 20
  • 1
    The code in the constructor does not belong there. – leppie Sep 29 '17 at 11:54
  • 15
    Your constructor is calling itself. Recursively. – Oded Sep 29 '17 at 11:55
  • 2
    Everytime you use `new Blagdani();` all this code is called **AGAIN**. – TheSkimek Sep 29 '17 at 11:55
  • This code should not be in that constructor. You create object in a constructor class of type of this object. – Arkadiusz Sep 29 '17 at 11:56
  • 1
    Why does a `Holiday` instance contain a `List`? Maybe that list should be `static` and you can create it in the static constructor. – Tim Schmelter Sep 29 '17 at 11:58
  • Ok I understand why now, but my next question then is: how to place it in the same class when I cannot call my instance of that class inside of it? My point is that I need to make separate records of those 2 properties for every entry in the list, and when I place it outside I cannot call that instance that I use for creating the record? – Marko Petričević Sep 29 '17 at 11:59
  • Do you really need `dan_u_tjednu`? Why not just `datum.DayOfWeek`? – Rufus L Sep 29 '17 at 12:04
  • @RufusL Kinda, coz it returns an english name for it, and everything inside my program is already made with my native language, thats why. – Marko Petričević Sep 29 '17 at 12:06
  • 1
    Oh, sorry, I assumed you were running under your native culture. In that case you'd have to do something like: `CultureInfo.GetCultureInfo("hr-HR").DateTimeFormat.GetDayName(datum.DayOfWeek)` – Rufus L Sep 29 '17 at 12:19

5 Answers5

1

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));
TheSkimek
  • 334
  • 1
  • 7
  • Ur solution works, only thing that I dont like in it, is creating a new class coz thats what I wanted to avoid when I asked a question. Thanks anyway =) – Marko Petričević Sep 29 '17 at 12:23
  • 1
    Well you dont have to create the ListManager class. You can keep the List in the Main class. That works too. Its hard to tell without context. But i guess you can figure the solution that suits you best. – TheSkimek Sep 29 '17 at 12:25
1

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,

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
  • The default initialization with the three holidays belongs into the static constructor because it's now a static list(which makes sense). Apart from that, you should explain what you have done and why it will fix the issue. Having said that, you should explain the issue as well. – Tim Schmelter Sep 29 '17 at 12:19
  • I was writing :) @TimSchmelter – Berkay Yaylacı Sep 29 '17 at 12:24
  • @Berkay Love ur explanation, I just have one more question for you: Ur telling me that if I dont set that list as static, it will only make one entry and that entry will be the last one I set (last holiday, coz thats what blagdan means ;) ) – Marko Petričević Sep 29 '17 at 12:29
  • Not last entry, you will have 3 count, but all of their properties will be same. For example all of them has same date and same dan_u_tjednu ("Petak"). @MarkoPetričević – Berkay Yaylacı Sep 29 '17 at 12:32
  • @Berkay yea 3 counts, thats right but it will always be 3 same ones. But how does static prevent that from happening here? Whats the catch? – Marko Petričević Sep 29 '17 at 12:34
  • Take a look at [this](https://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-c-when-to-use-it-why-cant-i-declare-th) @MarkoPetričević – Berkay Yaylacı Sep 29 '17 at 12:36
1

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

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Got explanation from 2 posts, thats why I love stackoverflow and its community, thats again =) – Marko Petričević Sep 29 '17 at 12:43
  • 1
    You're welcome! Please note that this differs from the accepted answer in a few ways. With this answer, you can use the list immediately, without creating any instance, using: `Blagdani.Blagdani_lista`. With the accepted answer, if you access the list without creating an instance first, the list is empty. If you create one instance, then the list is correct. But then if you happen to create *another* instance, the list will be twice the size, with duplicate items. This is why I recommend the `static` constructor rather than specialized instance constructors. – Rufus L Sep 29 '17 at 16:34
0

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.

Kyle Burns
  • 1,164
  • 7
  • 16
0
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);

    }