PLS HELP: Using events in inherited classes

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hi,

Consider i have this code:
// ---------------------------------------------------------------------
public delegate void DoSearchEventHandler(Form f, DoSearchEventArgs e);
public class DoSearchEventArgs
{
public String m_sIDColumn;
public String m_sQuery;
public DoSearchEventArgs(String sQuery, String sIDColumn)
{
m_sQuery = sQuery;
m_sIDColumn = sIDColumn;
}
}
public class SearchForm: Form
{
public event DoSearchEventHandler DoSearchEvent;
public bool DoSearchEventEmpty
{
get { return DoSearchEvent == null; }
}
public void DoSearchEventFire(Form f, DoSearchEventArgs e)
{
DoSearchEvent(f, e);
}
}
//----------------------------------------------------------------------
Now i have my form inherited from SearchForm and in some func of this
new form i want to use that event "DoSearchEvent":

public class MyForm: SearchForm
{
public void Search()
{
if (DoSearchEvent != null)
DoSearchEvent(this, new DoSearchEventArgs("",""))

}
}

Compiler gives an error on line "if (DoSearchEvent != null)" that you
can only use += or -= for events outside the class where they were
declared.
But MyClass is inherited from SearchClass so why events don't get
inherited as well?

Currently i have to declare two functions for each of those events in
SeachForm class: SetEvent and FireEvent, so for 10 events i have 20
pretty much redundant functions which differ by sematics but same by sence.

What should be the best way out?


Thank you in advance,
MuZZy
 
Hi MuZZy,

Thank you for posting.

If a base class contains an event variable, you can access it in a derived
class directly without using any object instance as a prefix. However, you
must use += or -= for the event variable outside the base class, which even
includes the derived class. It's a syntax.

I think you could define a "common" function to fire the events which have
the same signature in the base class. You can use a Dictionary to store the
event instances. In the common function to fire the events, you should get
the delegate instance out from the Dictionary and fire the event.

Here is a sample as follows.

public delegate void Delegate1(int i);

public class PropertyEventsSample
{
private System.Collections.Generic.Dictionary<string,
System.Delegate> eventTable;

public PropertyEventsSample()
{
eventTable = new
System.Collections.Generic.Dictionary<string, System.Delegate>();
eventTable.Add("Event1", null);
eventTable.Add("Event2", null);
}

public event Delegate1 Event1
{
add
{
eventTable["Event1"] = (Delegate1)eventTable["Event1"]
+ value;
}
remove
{
eventTable["Event1"] = (Delegate1)eventTable["Event1"]
- value;
}
}

public event Delegate1 Event2
{
add
{
eventTable["Event2"] = (Delegate1)eventTable["Event2"]
+ value;
}
remove
{
eventTable["Event2"] = (Delegate1)eventTable["Event2"]
- value;
}
}

// this is the common function to fire the two events
internal void FireDelegate1Event(string eventname,int i)
{
Delegate1 D;
if (null != (D = (Delegate1)eventTable[eventname]))
{
D(i);
}
}
}

public class TestClass
{
public static void Delegate1Method(int i)
{
System.Console.WriteLine(i);
}

static void Main()
{
PropertyEventsSample p = new PropertyEventsSample();

p.Event1 += new Delegate1(TestClass.Delegate1Method);

p.FireDelegate1Event("Event1",2);

p.Event2 += new Delegate1(TestClass.Delegate1Method);

p.FireDelegate1Event("Event2",4);

Console.ReadLine();
}
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Back
Top