RaiseEvent in c#

Last post 11-04-2009 3:58 PM by RichardD. 8 replies.

Sort Posts:

  • RaiseEvent in c#

    10-28-2009, 6:11 AM
    • Member
      1 point Member
    • falz
    • Member since 07-04-2007, 9:48 PM
    • Posts 37

    Hi guys, Im having problems converting my vb.net code into c# in order to achieve a raiseEvent like the following

    In the dataOperations class I have the following event (1 of many)

    Public Event returnUserDetails(ByVal name As String, ByVal username As String, ByVal pk As Integer)

    then in a function were I process the user details search query from the database I call the event like

    RaiseEvent returnUserDetails(name, username, pk)

    now in the other class where I want to read these values I simply declare an object of type dataOperations class, and get the following method, which gives me access to the values


    Protected Sub oDB_returnUserDetails(ByVal name As String, ByVal username As String, ByVal pk As Integer) Handles oDB.returnUserDetails

    End Sub


    .. I was wondering if its done in a similar way in c# as I did try to look up tutorials, but am confused with the delegates and how to create a simple event like the one I explained up in my post.


    Thanks


  • Re: RaiseEvent in c#

    10-28-2009, 8:10 AM
  • Re: RaiseEvent in c#

    10-28-2009, 8:19 AM
    • Contributor
      4,931 point Contributor
    • karthicks
    • Member since 01-09-2009, 12:43 PM
    • Chennai
    • Posts 852

     to raise  event in c# no need to use vb.net keyword RaiseEvent

    just call like below

    returnUserDetails(name, username, pk);

    Refer : http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx

    If this post is useful,
    Please,Dont Forget to click Mark As Answer and Earn Your Point

    Thanks,
    Karthick S
    Celebrate Programming
  • Re: RaiseEvent in c#

    10-28-2009, 8:38 AM
    • Member
      1 point Member
    • falz
    • Member since 07-04-2007, 9:48 PM
    • Posts 37

    Hi, many thanks for your replies


    Im still stuck..

    should the event declaration be as follows ?

    public delegate void returnMessage(object sender, EventArgs e);

    or like vb.net public event returnMessage(string message);

    I want to be able to pass in the message myself depending on the result of the function..

  • Re: RaiseEvent in c#

    10-28-2009, 8:52 AM
    • Member
      188 point Member
    • binli0114
    • Member since 09-26-2006, 12:31 AM
    • Sydney
    • Posts 48
    public delegate void returnMessage(Object sender, EventArgs e);
    public event returnMessage myReturnMsg;



    ------------------------------------------------------------
    I LOVE THIS GAME
  • Re: RaiseEvent in c#

    10-28-2009, 9:16 AM
    • Member
      1 point Member
    • falz
    • Member since 07-04-2007, 9:48 PM
    • Posts 37

    but how will i return the message.. as in I need to achieve something on the lines of


    public event returnMessage myReturnMsg (string message);  - even though i know (string message) isn't valid..


    but in my function


            private void returnDetails(string _firstName, string _lastName)
            {
                //processing here
                //when complete, raise the event to return message "Hello _firstName & _lastName"
    
                myReturnMsg(); // should the deletegate have the (string message) instead?
            }


  • Re: RaiseEvent in c#

    10-28-2009, 9:26 AM
    • Contributor
      4,931 point Contributor
    • karthicks
    • Member since 01-09-2009, 12:43 PM
    • Chennai
    • Posts 852

    based on your code that delegate is having one string argument, so you can pass directly like below

    myReturnMsg("Hello _firstName & _lastName");

    that string will be returned as argument where you are registering the event

    else, you can define you own eventargs class by deriving from System.EventArgs with your own properties and pass that object  

    If this post is useful,
    Please,Dont Forget to click Mark As Answer and Earn Your Point

    Thanks,
    Karthick S
    Celebrate Programming
  • Re: RaiseEvent in c#

    10-28-2009, 9:41 AM
    • Member
      1 point Member
    • falz
    • Member since 07-04-2007, 9:48 PM
    • Posts 37

    Here's the code of what im trying to do, i removed all the extra stuff but this should give you an exact image of what im trying to achieve



    //Inside dbOperations.cls
    
    	public class clsDBOperations
        	{
    
    		public delegate void returnMessage(string message);  
       		public event returnMessage myReturnMsg;  
    
    
    		public void returnDetails(string _firstName, string _lastName)  
     		{  
         			//processing here  
         			//when complete, raise the event to return message "Hello"  
       
         			myReturnMsg("Hello"); 
     		} 
    			
    	}
    
    
    //Inside Home.aspx
    
    	public partial class Home : System.Web.UI.Page
        	{
    		dbOperations dbo = new dbOperations();
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                dbo.returnUserDetails();
            }
    
     public void dbo_myReturnMsg(string message)
    		{
        			Response.Write(message);
    		}
    
    
    	}



  • Re: RaiseEvent in c#

    11-04-2009, 3:58 PM
    • Contributor
      2,714 point Contributor
    • RichardD
    • Member since 09-03-2002, 7:43 AM
    • Sussex, UK
    • Posts 358

    Try something like this:

    public class clsDBOperations
    {
    	public delegate void returnMessage(string message);
    	public event returnMessage myReturnMsg = delegate { };
    	
    	public void returnDetails(string _firstName, string _lastName)
    	{
    		...
    		myReturnMsg("Hello");
    	}
    }
    
    public partial class Home : System.Web.UI.Page
    {
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		dbOperations dbo = new dbOperations();
    		dbo.myReturnMsg += dbo_myReturnMsg;
    		dbo.returnUserDetails();
    	}
    	
    	public void dbo_myReturnMsg(string message)
    	{
    		Response.Write(message);
    	}
    }

    You need to subscribe to the event - dbo.myReturnMsg += dbo_myReturnMsg - in order to see it.

    Also, when you raise an event, you need to make sure there are subscribers. You can either check to see whether the event is null - if (null != myReturnMsg) myReturnMsg("Hello"); - or add an empty handler within the class.


    The standard convention with .NET is that event handlers should follow the same syntax. The first parameter should be an object called "sender", and the second should be a type derived from EventArgs. Also, if your class is not sealed, there should be a protected virtual method called "OnEventName" which is responsible for raising the event. This means that derived classes can raise the event, and can also override the method to receive notification when the event is raised. Combining these guidelines with the recommended casing rules would give something like this:

    public class ReturnMessageEventArgs : EventArgs
    {
    	private readonly string _message;
    	
    	public ReturnMessageEventArgs(string message)
    	{
    		_message = message;
    	}
    	
    	public string Message
    	{
    		get { return _message; }
    	}
    }
    
    public class DBOperations
    {
    	public event EventHandler<ReturnMessageEventArgs> MyReturnMsg;
    	
    	protected virtual void OnMyReturnMsg(ReturnMessageEventArgs e)
    	{
    		EventHandler<ReturnMessageEventArgs> handler = MyReturnMsg;
    		if (null != handler) handler(this, e);
    	}
    	
    	public void ReturnUserDetails()
    	{
    		...
    		OnMyReturnMessage(new ReturnMessageEventArgs("Hello"));
    	}
    }
    
    public partial class Home : System.Web.UI.Page
    {
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		DBOperations dbo = new DBOperations();
    		dbo.MyReturnMsg += dbo_MyReturnMsg;
    		dbo.ReturnUserDetails();
    	}
    	
    	public void dbo_MyReturnMsg(object sender, ReturnMessageEventArgs e)
    	{
    		Response.Write(e.Message);
    	}
    }
Page 1 of 1 (9 items)
Microsoft Communities