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
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