Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve a child class instance as sender in its base class event args?
    primarykey
    data
    text
    <p>I have a class called <code>DataModel</code> that implements the <code>INotifyDataErrorInfo</code> interface. In my app all my Models inherit from this app so that I don't have to code error handling logic nor change notification in any other classes. How can I raise the base class event below but send the child class that called it as a sender? I want to minimize repetitive code and would wish to avoid the whole virtual and override scenario.</p> <pre><code>virtual void SetErrors(string propertyName, List&lt;string&gt; propertyErrors) { //clear any errors that already exist for this property errors.Remove(propertyName); //add the list of errors for the specified property errors.Add(propertyName, propertyErrors); //raise the error notification event if (ErrorsChanged != null) ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName)); } </code></pre> <h2>Edit- Question is not clear enough</h2> <p>For example say I have this class</p> <pre><code>public class GangMember : Models.Base_Classes.DataObject { int _age; public int Age { set { _age = value; if (value &lt; 0) { List&lt;string&gt; errors = new List&lt;string&gt;(); errors.Add("Age can not be less than 0."); SetErrors("Age", errors); } } } } </code></pre> <p>When <code>SetErrors()</code> is called in my base class <code>DataModel</code> it raises it's event <code>ErrorsChanged</code> and passes it an instance of itself <code>this</code>. How can I get a reference to the child class <code>GangMember</code> in this situation?</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.
    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