0

Hello I am pretty new to C# so I'm not entirely sure what I'm trying to do is possible,

I am basically trying to set the get property on a List, when this list loads it will read information from another list, the methodology is:

If the parent list is empty , create a new list Otherwise iterate through the parent lists elements and process each accordingly

However the get method results in a stack overflow, running it through a debugger it just seems to run over and over again until the stack is blown even though it has hit and processed my return statement

Here is the code:

public ICollection<History> History
    {
      get {
            if (HistoryKeyCount == 0)//HistoryKeyCount = Count of Parent List
            {
                    History = new List<Histories>();
            }
            else
            {
                History = new List<Histories>();//Created again because after each read object is dropped from memory
                foreach (HistoryKey x in ParentList)
                {
                    if (x.Key == null)
                    {
                        // Do nothing
                    }
                    else
                        History.Add(ObjectFinder.FindObject<ParentList>(x.Key));
                }

            }
            return History;
        }
        set {
            //Not implemented yet
            }
      }

What happens is the it enters the get method, reaches the if statement, creates the new object , returns it then does it over and over again until a stackoverflow exception occurs

The problem is not in the ObjectFinder class or the creating the new methods, these aren't even reached when the error occurs.

I originally thought that it was a new object was being created after each instance of the if statement however after adding in a flag so that it would only be created once the problem still persisted.

Has anyone ever encountered this problem before? Because it has me stumped !!

----EDIT--- I forgot to mention that I am using the naked objects framework for .NET which could very well be the source of the problem

scrineym
  • 759
  • 1
  • 6
  • 28
  • Where are you calling the get method? Post that code. – Anirudh Ramanathan Jun 25 '12 at 01:42
  • Also post the definition of `History` – Tom Studee Jun 25 '12 at 01:43
  • 1
    Can we assume that `History` derives from `ClaimHistory`? – Kane Jun 25 '12 at 01:47
  • I am not in control of where the get method is executed I am using the NakedObjects framework for .Net which handles the get/set of all objects , this could very well be the problem. Also History is just one object with an int called Id and a string which contains a key, I'm afraid I'm not allowed to post the code (company policies and all that ) – scrineym Jun 25 '12 at 01:48
  • No claimhistory is the result of me badly censoring code !! ClaimHistory and History are the same object I just had to rename it, apologies – scrineym Jun 25 '12 at 01:50
  • 1
    As Kieth recommended, you should probably create a private backing field to store the value of `History` in, and then have `History` reference that, not itself, in the getter. – Jon Senchyna Jun 25 '12 at 02:02

1 Answers1

4

you call History within History, so it become recursive.

History.Add(ObjectFinder.FindObject<ParentList>(x.Key));

will call back into your property, and end up at the same line again, and then call itself again.... until you run out of stack

also at the end, you call back into History again..... it can't escape :)

I got a feeling you either want a local variable, to hold a collection to return, or you want a private field to hold the list. either way, don't call History within History

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I can see where you're coming from however when I comment out the constructors and just leave the return statement and blank if else blocks the exception still occurs, unless I just misunderstood your point. – scrineym Jun 25 '12 at 01:57
  • 1
    the return statement returns History, which is ITSELF, so it calls straight back into the property getter, it can't ever leave – Keith Nicholas Jun 25 '12 at 01:58
  • if you are going to store the list in a private field, ie a private called _History... then do _History.Add() and at the end return _History – Keith Nicholas Jun 25 '12 at 02:01
  • 1
    Thank you so much, I would've been stuck for hours on that, thank you so much for your help !! – scrineym Jun 25 '12 at 02:05