473,399 Members | 2,278 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,399 software developers and data experts.

How to work with inheritance of Delegate and Event

Hi All,

There is a scenario, in which there is a counter in my base class.
There is some initial value of this counter (check below code). Now
there is a function "DecreaseCount", which will decrease count. Now I
want some functionality like when this counter become zero(0) a
delegate will be fired of derived class. This is working fine.

Now, on my UI side this instance of Tester and Developer are a
node of a TreeView. There are lots of other instance of same classes in
that TreeView. If any company having more than one(0) Employee, the
color of TreeNode should be Green. And when it becomes to Zero(0) then
color of TreeNode should become Red.

This useless to pass every time a TreeNode to Employee class. Now
I want to write a code to change the color in my UI part. And want to
call this Method/Event from by Super (base) class when the counter
becomes Zero....

The following is my effort.....
Give your feedback....

/////////// Code Start /////////////
using System;
using System.Windows.Forms;

namespace DelegateInhertanceTest
{
public delegate void NotifierCall();

public abstract class Employee
{
//public delegate void ChangeNotifier();
NotifierCall notifierCall;
int counter = 5;

//Constructor
public Employee()
{
notifierCall = new NotifierCall(this.NotifyCall);
}

public Employee(int counter)
{
this.counter = counter;
notifierCall = new NotifierCall(this.NotifyCall);

}

public abstract void NotifyCall();

public virtual void NotifyCall()
{
MessageBox.Show("Call of Delegate In Base");
}

public void DecreaseCount()
{
this.counter--;
if (this.counter == 0)
{
this.notifierCall();
}
}
}

public class Tester : Employee
{
//Default Constructor
public Tester()
{
}

public override void NotifyCall()
{
MessageBox.Show("NotifyCall of Delegate In Derived of Tester");
}
}

public class Developer : Employee
{
//Default Constructor
public Developer()
{
}

public override void NotifyCall()
{
MessageBox.Show("NotifyCall of Delegate In Derived of Developer");
}

}
public class EmployeeDemo
{
static void Main()
{
Employee testers = new Tester();
Employee developers = new Developer();

while(true)
{
testers.DecreaseCount();
}
}

public void NotifyCall()
{
MessageBox.Show("Call of induvidual instance");
}
}
}
////// Code End //////
Thanks In Advance.....
Waiting for your feedback....

Thanks & Regards,
Rushikesh
Integreon

Nov 17 '05 #1
3 1463
One more thing.

Should I resolve this using creting an Public Event....

If yes, then How???
Please reply back to me

Thanks & Regards,
Rushikesh
Integreon

Nov 17 '05 #2
Hi Roshik
as far as i understood ur question heres my answer . Basically u need
to create ur own eventsArgs Class which will have a colour as parameter
and that will be set in the base class. When the ColourChangeEvent will
be fired, whatever the color is set in the parent class will be passed
to eventhandler for Inheriting class which will then set its colour
namespace DelegateInhertanceTest
{
public delegate void ChangeColour(ColourEventArgs e);
public event ChangeColour OnColourChangeEvent;

public abstract class Employee
{
private int intCount;
public int EmployeeCount
{
get { return this.intCount;}
set { this.intCount = value;}
}

public void DecreaseCount()
{
this.counter--;
if (this.Count == 0)
{
e = new ColourArgs("green");
this.OnColourChangeEvent(e);
}
}

public abstract OnColourChangeEvent(ColourEventArgs e);
}
public class Tester : Employee
{
private string nodeColour;
//Default Constructor
public Tester()
{
}
public override void OnColourChangeEvent(ColourArgs e)
{
nodeColour = e.GetColour;
}
public override void NotifyCall()
{
MessageBox.Show("NotifyCall of Delegate In
Derived of Tester");
}
}

public class ColourEventArgs : EventArgs
{
ColourEventArgs (string clr)
{
this.Colour = clr;
}
private string strColour;
public string Colour
{
get {return strColour;}
set {this.strColour = value;}
}
}
}

public class EmployeeDemo
{
static void Main()
{
Employee testers = new Tester();
if ( testers.EmployeeCount > 0)
{
}
while(true)
{
testers.DecreaseCount();
}
}

}

hope it helps
Vivek

Nov 17 '05 #3
Thanks Vivek,

You understand correctly, but only problems is how do my UI(Say Main
function) get notify about this. Because the color change will fier
automatically when count becomes Zero....
But how do I hange the Color of particular TreeNode.
LoveDotNet wrote:
Hi Roshik
as far as i understood ur question heres my answer . Basically u need
to create ur own eventsArgs Class which will have a colour as parameter
and that will be set in the base class. When the ColourChangeEvent will
be fired, whatever the color is set in the parent class will be passed
to eventhandler for Inheriting class which will then set its colour
namespace DelegateInhertanceTest
{
public delegate void ChangeColour(ColourEventArgs e);
public event ChangeColour OnColourChangeEvent;

public abstract class Employee
{
private int intCount;
public int EmployeeCount
{
get { return this.intCount;}
set { this.intCount = value;}
}

public void DecreaseCount()
{
this.counter--;
if (this.Count == 0)
{
e = new ColourArgs("green");
this.OnColourChangeEvent(e);
}
}

public abstract OnColourChangeEvent(ColourEventArgs e);
}
public class Tester : Employee
{
private string nodeColour;
//Default Constructor
public Tester()
{
}
public override void OnColourChangeEvent(ColourArgs e)
{
nodeColour = e.GetColour;
}
public override void NotifyCall()
{
MessageBox.Show("NotifyCall of Delegate In
Derived of Tester");
}
}

public class ColourEventArgs : EventArgs
{
ColourEventArgs (string clr)
{
this.Colour = clr;
}
private string strColour;
public string Colour
{
get {return strColour;}
set {this.strColour = value;}
}
}
}

public class EmployeeDemo
{
static void Main()
{
Employee testers = new Tester();
if ( testers.EmployeeCount > 0)
{
}
while(true)
{
testers.DecreaseCount();
}
}

}

hope it helps
Vivek


Nov 17 '05 #4

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

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
7
by: StevenVibert | last post by:
I'm rewriting a C++ TAPI app I wrote a while ago in C#. Everything works fine for the first call. Unfortunately, all subsequent calls are completely ignored by TAPI until I restart the app again....
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
8
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
64
by: groups | last post by:
C# is an impressive language...but it seems to have one big limitation that, from a C++ background, seems unacceptable. Here's the problem: I have a third-party Document class. (This means I...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.