Note that there are some explanatory texts on larger screens.

plurals
  1. PODelegates and inheritance in c#
    primarykey
    data
    text
    <p>I have a bit of a problem with inheritance of delegates. Delegate as far as i understand is a pointer to a pair: an instance and a method, the thing is this is the method referenced in the creation of the delegate, and not affected by inheritance. So, this won't work:</p> <pre><code>public class BaseObject { public delegate void del(); public BaseObject() { next=Method; } public del next; public void ExecuteNext() { next(); } public virtual void Method() { Debug.Log("BASE"); } } public class InheritedObject:BaseObject { override public void Method() { Debug.Log("INHERITED"); } } ... (new InheritedObject()).ExecuteNext(); </code></pre> <p>Execute runs the base Method(), I want it to run the inherited Method(). I have found some way around, but it is inefficient, distracting and very error prone, anyway here's the current working version that I'd like to get rid of:</p> <pre><code>class BaseObject { public delegate void del(); BaseObject() { next=DoMethod; /// using DoMethod here } public del next; public void ExecuteNext() { next(); } public void DoMethod() { /// using DoMethod here Method(); } public virtual void Method() { // do base } } class InheritedObject:BaseObject { override public void Method() { // do inherited } } ... (new InheritedObject()).Execute(); </code></pre> <p>This DoMethod aproach works but has many problems, </p> <ol> <li>lots of useless code</li> <li>error prone when using the class - easy to mistake obj.next=<code>DoMethod</code> with obj.next=<code>Method</code></li> <li>error prone when inheriting the class - I have to remember to not to inherit the DoMethod, and lots of virtual and overrides.</li> </ol> <p>Any suggestions how can I do that better? Perhaps some annotation magic that does the <code>DoMethod</code> by itself? I've already thought of dictionaries - they aren't good here, they add even another level of confusion (Mono, .NET 2, Unity3d framework)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload