473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

PLS HELP: Using events in inherited classes

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
Jun 9 '06 #1
2 1377
The best way of handling this is to put a protected method that raises the
event in the base. The derived class can then call that method which tells
the base to raise the event.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung/
"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:uf**************@TK2MSFTNGP05.phx.gbl...
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

Jun 9 '06 #2
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.
================================================== ==

Jun 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
6
by: David Lozzi | last post by:
Howdy, I'm new to classes. Below is my class User. (is this a reserved namespace or class?) It works great, kind of. If I specify the username and password, the correct firstname and lastname...
10
by: Chad Miller | last post by:
I currently have a base form that I inherit. The base for has a custom event. The event will not raise threw the inherited form. I was wondering if events work threw inheritance or should I use...
2
by: chetsjunk | last post by:
I am having problems with an inherited class not handling an event for the class it is inherited from. But it's not constistent, so I'm guessing the problem is elsewhere in my code. I created the...
6
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.