How to work with inheritance of Delegate and Event

I

IMS.Rushikesh

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
 
I

IMS.Rushikesh

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
 
L

LoveDotNet

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
 
I

IMS.Rushikesh

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top