473,320 Members | 1,831 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,320 software developers and data experts.

raising an event . .. and getting a callback? to the originating object

I"m trying to figure out what concept I'm missing here, or if its not a good
idea .. or what.

Here is my example.. code is below.

I have an employee class.
It has an event that can be raised.
In the constructor, I check the to see if the employees birthday is today.
If it is today, then I raise an event.

I have a HumanResources class.
HumanResources creates an Employee class, providing the date of birth.
HR also wires up an event , to handle the case where the dob is today.

Employee raises the event.
HR responds to the event.

The question on the table is :::
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?
Is it a callback? Or what key concept am I missing?

Is this too tight of a coupling? or cyclic reference? or some other
gotcha?

Below is sample code.
Everything is on order I think.. the thing sticking out ... related to my
question is the
SubscribersToMyEventGotTheirEvent() method? aka, I have no idea if I'm
barking up the right tree here.
Most times I see the sender as "object sender"...
I guess I could cast it .. or change its object type. But that seems like
tight coupling.
I think java has a ICallback interface or something (long time since I
java'ed)


public class Employee

{
public delegate void BirthdayOccuredEventHandler(object sender,
EmployeeBirthdayArgs args);
public event BirthdayOccuredEventHandler BDayOcurred;
public Employee(int empid , DateTime dateOfBirth)
{
this.CheckForBDay(empid , dateOfBirth);
}

private void CheckForBDay(int empid , DateTime dob)
{

if (null!=BDayOcurred)
{
DateTime now = DateTime.Now;

if( dob.Month == now.Month && dob.Day == now.Day)
{
//today! is your birthday, raise the event

BDayOcurred(this, new EmployeeBirthdayArgs(empid, dob));
}
}

}

public void SubscribersToMyEventGotTheirEvent()
{

// Can I know somehow that my subscribers got their messages successfully,
and nothing exceptioned out??

}

}//End Employee class

public class EmployeeBirthdayArgs : EventArgs {
public EmployeeBirthdayArgs( int empid , DatTime dob ) {
this.EmployeeID = empid;
this.DateOfBirth = dob ;
}

public int EmployeeID; // i'm not using properties to shorten the example
public DateTime DateOfBirth;// i'm not using properties to shorten the
example

} // End EventArgs class

public class HumanResources()
{
public void Go()
{
Employee e = new Employee(123 , DateTime.Now)
e.BDayOcurred += new
BirthdayOccuredEventHandler(RespondToABirthdayEven t);
}

public void RespondToABirthdayEvent(object sender , EmployeeBirthdayArgs
args)
{

Console.Writeline ( arg.EmployeeID.ToString() + " " +
arg.DateOfBirth.ToShortDateString());
//this doesn't exist, but lets put a fake call into something like
"SendCard"
CardFactory.SendCard ( args );
}
}//end human resources

Jul 26 '06 #1
4 1512
>In the constructor, I check the to see if the employees birthday is today.
>If it is today, then I raise an event.
An event on that instance? if so, nothing will have had chance to subscribe
yet...
>Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?
Event invocation (using the default approach) runs by executing the
delegates **on the current thread**; as such, when after you have invoked
the delegate you know that the chain has been invoked and completed.
>BDayOcurred(this, new EmployeeBirthdayArgs(empid, dob));
This is dodgy; if you have no subscribers it will throw a null-object
exception; should be something like (following the common OnSomething
pattern):

// could be protected virtual if inheritance is likely
private void OnBDayOccurred(int empid) { // don't need param if available on
a field
BirthdayOccuredEventHandler handler = BDayOcurred;
if(handler!=null) handler(this, new EmployeeBirthdayArgs(empid, dob));
}

Then you just call "OnBDayOccurrent(empid);" at the point you want to use
it.
Employee e = new Employee(123 , DateTime.Now)
e.BDayOcurred += new...
Too late; missed it. You would either need to pass in the delegate into the
ctor (eugh), or (my preferred option) something like:

Employee e = new Employee();
e.BDayOccurred += new...
e.Initialise(123, DateTime.Now);

Although even then it seems an odd way of going about it... perhaps a better
option would be to have the event static on the Employee class; you
subscribe once (independent of any individual Employees), and when the ctor
detects a birthday it invokes the static event, passing the details of the
employee in question (as current). You would need the On... method to accept
the sender and use this in place of "this", but other than that it would
hold some water...

Does this help?

Marc

Jul 26 '06 #2
sloan wrote:
I have an employee class.
It has an event that can be raised.
In the constructor, I check the to see if the employees birthday is today.
If it is today, then I raise an event.
As Marc already pointed out, no one will ever get that event. Raising
the event in the constructor is too early, as no one will have had the
chance to subscribe to it yet.
The question on the table is :::
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?
When you raise the event and it returns, you can be sure that all
current subscribers have received it. The problem in your case is that
there cannot be any subscribers yet because you are raising the event in
the constructor.

Is the constructor the only place where you have to check for the
birthday? Or will the event also be raised if the application is running
for a few days and suddenly it figures out that an employee has birthday
today?

I would just add a boolean property to the employee class called
"HasBirthdayToday". So you can write:

Employee e = new Employee(DateTime.Now);

if (e.HasBirthdayToday) {
// celebrate birthday
}

... additionally you can also use the event (for the case that it can
also be raised after a few days).

hth,
Max
Jul 26 '06 #3
Ok.. I screwed up using hte Constructor.

That wasn't my main focus.. I was typing out a quick sample, and got that
screwed up.
Employee e = new Employee();
e.BDayOccurred += new...
e.Initialise(123, DateTime.Now);
I see that's the way to do that part of it.

My question still remains about the callback.


"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
In the constructor, I check the to see if the employees birthday is
today.
If it is today, then I raise an event.

An event on that instance? if so, nothing will have had chance to
subscribe
yet...
Is there a way to let the Employee object .. know that all subscribers of
the said raisedEvent, got those messages?

Event invocation (using the default approach) runs by executing the
delegates **on the current thread**; as such, when after you have invoked
the delegate you know that the chain has been invoked and completed.
BDayOcurred(this, new EmployeeBirthdayArgs(empid, dob));

This is dodgy; if you have no subscribers it will throw a null-object
exception; should be something like (following the common OnSomething
pattern):

// could be protected virtual if inheritance is likely
private void OnBDayOccurred(int empid) { // don't need param if available
on
a field
BirthdayOccuredEventHandler handler = BDayOcurred;
if(handler!=null) handler(this, new EmployeeBirthdayArgs(empid, dob));
}

Then you just call "OnBDayOccurrent(empid);" at the point you want to use
it.
Employee e = new Employee(123 , DateTime.Now)
e.BDayOcurred += new...

Too late; missed it. You would either need to pass in the delegate into
the
ctor (eugh), or (my preferred option) something like:

Employee e = new Employee();
e.BDayOccurred += new...
e.Initialise(123, DateTime.Now);

Although even then it seems an odd way of going about it... perhaps a
better
option would be to have the event static on the Employee class; you
subscribe once (independent of any individual Employees), and when the
ctor
detects a birthday it invokes the static event, passing the details of the
employee in question (as current). You would need the On... method to
accept
the sender and use this in place of "this", but other than that it would
hold some water...

Does this help?

Marc

Jul 26 '06 #4
My question still remains about the callback.

Really? It has been answered by two respondants already... as long as you
just use the simple event invoke [i.e. somehandler(this, someargs)] then as
soon as that line completes all subscribers have received and processed the
events. Because you just did it! (i.e. it ran on your thread).

If you are doing something more complex (which your code doesn't show), then
yes you might need some kind of callback, but how you would implement this
would depend on the details.

So: why do you think you need the callback? What is this trying to achieve?
(genuine question - not meant critically; without that info its hard to give
a better answer).

Marc
Jul 26 '06 #5

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

Similar topics

2
by: Jo Voordeckers | last post by:
Hello all, I'm pretty new to the Java newsgroups so I apologize for dropping this into several maybe offtopic groups. I'm sorry! So on to my problem... I've come to a point in our RMI...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
3
by: L | last post by:
Hi, I have a windows form say "Form1" which has a menuitem "SetUp". When I click this item, a new form "SetUpForm" opens up. When I change the fields in the "SetUpForm" and click the "OK"...
3
by: Jon Turner | last post by:
I have an asynchronous call to a remote procedure that is invoked thru BeginInvoke. The problem is that in the Delegate if I throw an event before the CallBack function exits, the CallBack will...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.