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.

9
  • 1
    The code in the constructor does not belong there. Commented Sep 29, 2017 at 11:54
  • 15
    Your constructor is calling itself. Recursively. Commented Sep 29, 2017 at 11:55
  • 2
    Everytime you use new Blagdani(); all this code is called AGAIN. Commented Sep 29, 2017 at 11:55
  • 1
    Why does a Holiday instance contain a List<Holiday>? Maybe that list should be static and you can create it in the static constructor. Commented Sep 29, 2017 at 11:58
  • 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) Commented Sep 29, 2017 at 12:19

5 Answers 5

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,

Sign up to request clarification or add additional context in comments.

6 Comments

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.
I was writing :) @TimSchmelter
@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 ;) )
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 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?
|
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)); 

2 Comments

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 =)
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.
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

2 Comments

Got explanation from 2 posts, thats why I love stackoverflow and its community, thats again =)
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.
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.

Comments

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); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.