Simple example of raising an event

T

ThatsIT.net.au

I want to raise an event to pass some text back from a class to an aspx page
in C#

can anyone give me a simple example showing the basic syntax.

Thanks you
 
T

ThatsIT.net.au

To make things a bit clearer, can someone convert this VB example to C#, I
cant seem to work out the syntax


Partial Class test
Inherits System.Web.UI.Page


Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
oTest.getEvent()
End Sub

Public Sub getevent(ByVal tex As String) Handles oTest.Event1
Label1.Text = tex
End Sub


End Class

Public Class TestClass
Public Event Event1(ByVal tex As String)

Public Sub getEvent()
Dim e As EventArgs = New EventArgs
RaiseEvent Event1("Wow it works")
End Sub
End Class
 
A

Alberto Poblacion

ThatsIT.net.au said:
To make things a bit clearer, can someone convert this VB example to C#, I
cant seem to work out the syntax

I have written a translation below each block. Note that I have modified
your event arguments to conform to the established .Net practice of always
providing a "sender" argument and encapsulating all returned information in
a second argument that inherits from EventArgs.

Partial Class test
Inherits System.Web.UI.Page


Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
oTest.getEvent()
End Sub

Public Sub getevent(ByVal tex As String) Handles oTest.Event1
Label1.Text = tex
End Sub


End Class

public partial class test : System.Web.UI.Page
{
private TestClass oTest;

protected void Page_Load(object sender, EventArgs e)
{
oTest = new TestClass();
oTest.Event1 += new Event1Handler(getevent);
oTest.getEvent();
}

private void getevent(object sender, Event1EventArgs e)
{
Label1.Text = e.s;
}
}

Public Class TestClass
Public Event Event1(ByVal tex As String)

Public Sub getEvent()
Dim e As EventArgs = New EventArgs
RaiseEvent Event1("Wow it works")
End Sub
End Class

public class TextClass
{
public delegate void Event1Handler(object sender, Event1EventArgs e);
public event Event1Handler Event1;

public void getEvent()
{
if (Event1!=null)
{
Event1EventArgs sea = new Event1EventArgs();
sea.s = "Wow it works";
Event1(this, sea);
}
}
}

public class Event1EventArgs: EventArgs
{
public string s;
}
 
T

ThatsIT.net.au

Thank very much, I'm almost there but im getting an error on this line. any
ideas?

oTest.Event1 += new Event1Handler(getevent);

The type or namespace name 'Event1Handler' could not be found (are you
missing a using directive or an assembly reference?)


My Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test_EventTestC : System.Web.UI.Page
{
TestCS oTest ;

protected void Page_Load(object sender, EventArgs e)
{
oTest = new TestCS();
oTest.Event1 += new Event1Handler(getevent);
oTest.getEvent();
}

private void getevent(object sender, Event1EventArgs e)
{
Label1.Text = e.s;
}
}


My Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TestCS
{
public delegate void Event1Handler(object sender, Event1EventArgs e);
public event Event1Handler Event1;

public void getEvent()
{
if (Event1 != null)
{
Event1EventArgs sea = new Event1EventArgs();
sea.s = "Wow it works";
Event1(this, sea);
}
}
}

public class Event1EventArgs : EventArgs
{
public string s;
}
 
P

Pavel Minaev

ThatsIT.net.au said:
Thank very much, I'm almost there but im getting an error on this line.
any ideas?

oTest.Event1 += new Event1Handler(getevent);

The type or namespace name 'Event1Handler' could not be found (are you
missing a using directive or an assembly reference?)

You need to fully qualify nested types - so it should be "new
TestCS.Event1Handler(getevent)" here.

Or you can move the declaration of delegate type Event1Handler out of class
TestCS (delegate types for event handlers are typically declared as
top-level and not nested types, anyway).
 
T

ThatsIT.net.au

solved it
replaced
oTest.Event1 += new Event1Handler(getevent);
with
oTest.Event1 += getevent;
 
T

ThatsIT.net.au

Pavel Minaev said:
You need to fully qualify nested types - so it should be "new
TestCS.Event1Handler(getevent)" here.


thats did not work for me

I managed to get it working with
oTest.Event1 += getevent;

I'm from a VB background and although I do a lot of C# at work, I think this
is one area where VB is much more friendly.
Or you can move the declaration of delegate type Event1Handler out of
class TestCS (delegate types for event handlers are typically declared as
top-level and not nested types, anyway).


not sure what you mean here
 
A

Alberto Poblacion

ThatsIT.net.au said:
solved it
replaced
oTest.Event1 += new Event1Handler(getevent);
with
oTest.Event1 += getevent;

Yes, this is the automatic inference of delegate types, which lets you
omit the type of the delegate on the condition that the compiler has enough
information to infer it. In this case, it is inferred from the type of
Event1 on the left hand side of the assignment.
This is a new feature of C# 2.0; it would not have worked in C# 1.0,
which is the reason why I used the more complete syntax.
 
A

Alberto Poblacion

ThatsIT.net.au said:
thats did not work for me

Depending on where you declared the containing class, it may be using a
different namespace. If you are programming in Visual Studio, there is an
easy way to get the correct code: Type "oTest.Event1 +=" in the editor, and
Intellisense will offer a tooltip similar to "Press TAB to insert". If you
press TAB at this point, it will insert the correct delegate constructor in
your source code.
 
T

ThatsIT.net.au

Alberto Poblacion said:
Depending on where you declared the containing class, it may be using a
different namespace. If you are programming in Visual Studio, there is an
easy way to get the correct code: Type "oTest.Event1 +=" in the editor,
and Intellisense will offer a tooltip similar to "Press TAB to insert". If
you press TAB at this point, it will insert the correct delegate
constructor in your source code.

Yes done so, it worked. this is the syntax that Pavel suggested, but It did
not work them, I must of made a error. its working now.
oTest.Event1 += new TestCS.Event1Handler(oTest_Event1);

Where oTest_Event1 is the handler entered by intelligence

Thanks for your help.
I must say again, that VB is much more syntax friendly when it comes to
events
 

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