1

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();
}
Mich
  • 3,188
  • 4
  • 37
  • 85
  • 1
    of course it would. this is how inheritance works... when you have a child/parent relationship, all the children classes get called/initialised from the parent class. The default constructors get called. your code also seems incorrect. why would a child class create instances of parent? (Parent being Note here). – Ahmed ilyas Mar 05 '14 at 02:23
  • I see everywhere I looked this is the definition of a child class Note (Note : Bar) http://www.csharp-station.com/Tutorial/CSharp/lesson08 How should I create a Child class from a parent Class, I want the Child class to be Note. Thanks, – Mich Mar 05 '14 at 02:32
  • 1
    Are you sure that Note *extends* bar? – Gus Mar 05 '14 at 02:36
  • @user2056201 - invert your code. make Bar derive from Note – Ahmed ilyas Mar 05 '14 at 02:38
  • It looks like the relationship between Bar and Note should be "Has" rather than "Is" Have a look at some of the answers here: http://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language – Andrew Kennan Mar 05 '14 at 02:40

3 Answers3

1

Assuming this is talking about musical concepts, Note probably shouldn't inherit from Bar. Rather, a Bar has Note instances. If there is common behavior between the two, you may want a common base class or interface instead, like this:

public interface IPlayable
{
    void Play();
}

class Bar : IPlayable
{
    private IList<Note> notes = new List<Note> { new Note() }; 

    public void Play() 
    {
        foreach (var note in notes)
        {
            note.Play();
        }
    }
}

class Note : IPlayable
{        
    public Note()
    {
        //do stuff
    }

    public void Play() { /* ... */ }
}

Basically don't confuse parent/child relationships with inheritance relationships. Only use inheritance when two classes have common data or behaviors.

Jacob
  • 77,566
  • 24
  • 149
  • 228
0

You could always instantiate in the base class conditionally.

public void Main()
{
    Bar newBar = new Bar(true);
}

// Define other methods and classes here
class Bar
{
    List<Note> notes; 

    public Bar(bool instantiate)
    {
        if(instantiate) {
            notes = new List<Note>(0);
            notes.Add(new Note());
        }
    }
}

class Note : Bar
{        
    public Note() : base(false)
    {
        //do stuff
    }
}

Otherwise, you will have a problem with your code. Base class constructors are called before the derived class constructors, so in your code each time you call the Note() constructor, you are calling the Bar() constructor first, which calls Note() ect...

By instantiating the base class conditionally and having the derived class call the base class with the Bar(false) constructor, you will stop the cycle after the first iteration through.

This seems like a strange design decision to me. I get the feeling you are not quite sure what the purpose of class derivation is and how derived classes inherit the properties and methods of the base.

seangwright
  • 17,245
  • 6
  • 42
  • 54
  • I apologize for not clarifying my question, what I would like to do is instantiate 1 Instance of Parent class with multiple instances of the Child class. This way every time I create a Bar object, I would like it to create multiple Note objects. Is this possible to do with a derived relationship? Or do I need to make the two classes completely independent. – Mich Mar 05 '14 at 03:54
  • Derivation is used to represent a hierarchy of objects going from general to specific. If you have a class Animal and also a class Mammal : Animal, the Mammal is seen here as being a more specific type of Animal. It has some of the same functionality and properties as Animal (eat, sleep, height weight) but it has some the base class doesn't have (hair). It also possibly overrides how the base functionality and properties are implemented while still exposing the same names for these things. Does this relationship make sense for your Note and Bar? Is one a more specific version of the other? – seangwright Mar 05 '14 at 05:26
0

This is how I solved my issue

Not sure if this is the correct usage of delegates, but it worked for me

delegate void RemoveNoteDelegate(Note note);

class Bar
{
    private List<Note> notes; 

    public Bar()
    {
        notes = new List<Note>(0);
        notes.Add(new Note(removeNote))
    }

    public void removeNote(Note note)
    {
        notes.Remove(note);
    }
}

class Note
{   
    public RemoveNoteDelegate remove_Note;

    public Note(RemoveNoteDelegate remove_Note)
    {
        //do stuff
        remove_Note(this);
    }
}

public MainWindow()
{
    private Bar newBar = new Bar();
}
Mich
  • 3,188
  • 4
  • 37
  • 85
  • 1
    Your delegate here is not doing anything. Remove the delegate from your code and constructors and your Bar newBar = new Bar() will have the exact same functionality. This is because your notes.Remove(note) method isn't removing the note. This has to do with reference types and value parameters, I believe. Anyway, the delegate as it stands doesn't seem to be serving any purpose. – seangwright Mar 05 '14 at 07:16