I'm lost trying to solve this issue, below is an abridged example of my code. My issue is that when I instantiate a Note object from Bar object, the Bar constructor gets called again then creates another Note object and so on until I get a stack overflow error.
Is there a reason for this recursion and how can I create an instance of the child class correctly to prevent it?
EDIT: I am trying to achieve having one instance of the parent class Bar with multiple instances of child classes Note. This way every time I create the parent class Bar it will create it's own set of Notes. Does this have to be done with the classes written without any inheritance relationship (just a separate Bar and Note class)? I need to have a function inside the child class (I cannot move this function to parent class for other reasons) call a function in the parent class, that would destroy that instance of the child class with base.RemoveNote(this); Is there a better way of doing this or is there a way to destroy the instance of the child class from within the same instance of the child class?
Code:
class Bar
{
private List<Note> notes;
public Bar()
{
notes = new List<Note>(0);
notes.Add(new Note())
}
public void removeNote(Note note)
{
notes.Remove(note);
}
}
class Note : Bar
{
public Note()
{
//do stuff
base.RemoveNote(this);
}
}
public MainWindow()
{
private Bar newBar = new Bar();
}