[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

event handling in c#

if i want to do event handling, is there an easier way than to use System.EventHandler and System.EventArgs?  i am just writing an console exe program.  thanks.
0
mmingfeilam
Asked:
mmingfeilam
4 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
...and what events are wanting to trap in your console program?!

Are you talking about custom events from a class you wrote?  If yes, then you can use any signature you want (even no parameters!).
0
 
dj_alikCommented:
Like this;
A Timer class for C# Console Applications
http://www.mycsharpcorner.com/Post.aspx?postID=31
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
using System;

public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);

public class DivBySevenEventArgs : EventArgs
{
    public readonly int TheNumber;
   
    public DivBySevenEventArgs(int num)
    {
        TheNumber = num;
    }    
   
}

public class DivBySevenListener
{
    public void ShowOnScreen(object o, DivBySevenEventArgs e)
    {
        Console.WriteLine(
            "divisible by seven event raised!!! the guilty party is {0}",
            e.TheNumber);
    }    
}

public class BusterBoy
{
    public static event DivBySevenHandler EventSeven;
   
    public static void Main()
    {
        DivBySevenListener dbsl = new DivBySevenListener();
        EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
        GenNumbers();
    }
   
    public static void OnEventSeven(DivBySevenEventArgs e)
    {
        if(EventSeven!=null)
            EventSeven(new object(),e);
    }    
   
    public static void GenNumbers()
    {
        for(int i=0;i<99;i++)
        {
            if(i%7==0)
            {
                DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);
                OnEventSeven(e1);
            }
        }        
    }
       
}

more on this url

http://www.codeproject.com/KB/cs/csevents01.aspx
0
 
anarki_jimbelCommented:
I believe your  requirements are not quite clear.

Really, I believe guys have answered the question already. I.e., Idle_Mind answered.

Yes, you may have an event with any signature. No any arguments  - is OK.

However, I can't see any problems with arguments. Moreover, the standard signature for .Net events is:

(object sender, EventArgs e)

e - completely useless object. It contains NO information whatsoever.
sender - may not be used at all

However, this signature is a standard. Now I work with a big application. Some developers some time ago created events with no arguments. Now some additional information is required to be passed - custom event arguments are needed (that inherit from EvenArgs). But I also need to go through code and change signature everywhere. If created handlers had standard signature - I might have much less work. Can't repeat here words I'm telling to these lazy developers... :)

My advice: use standard signatures. It won't save you much time if you skip standard arguments...:)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now