471,342 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

Simple example of raising an event

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

Sep 2 '08 #1
9 1606
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
"ThatsIT.net.au" <me@workwrote in message
news:93**********************************@microsof t.com...
>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
Sep 2 '08 #2
"ThatsIT.net.au" <me@workwrote in message
news:15**********************************@microsof t.com...
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;
}

Sep 2 '08 #3
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;
}


"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:ee**************@TK2MSFTNGP06.phx.gbl...
"ThatsIT.net.au" <me@workwrote in message
news:15**********************************@microsof t.com...
>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;
}
Sep 2 '08 #4
"ThatsIT.net.au" <me@workwrote in message
news:0B**********************************@microsof t.com...
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).
Sep 2 '08 #5
solved it
replaced
oTest.Event1 += new Event1Handler(getevent);
with
oTest.Event1 += getevent;

"ThatsIT.net.au" <me@workwrote in message
news:0B**********************************@microsof t.com...
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;
}


"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:ee**************@TK2MSFTNGP06.phx.gbl...
>"ThatsIT.net.au" <me@workwrote in message
news:15**********************************@microso ft.com...
>>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;
}
Sep 2 '08 #6

"Pavel Minaev" <in****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"ThatsIT.net.au" <me@workwrote in message
news:0B**********************************@microsof t.com...
>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.

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

Sep 2 '08 #7
"ThatsIT.net.au" <me@workwrote in message
news:53**********************************@microsof t.com...
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.

Sep 2 '08 #8
"ThatsIT.net.au" <me@workwrote in message
news:10**********************************@microsof t.com...
>You need to fully qualify nested types - so it should be "new
TestCS.Event1Handler(getevent)" here.


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.
Sep 2 '08 #9

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP06.phx.gbl...
"ThatsIT.net.au" <me@workwrote in message
news:10**********************************@microsof t.com...
>>You need to fully qualify nested types - so it should be "new
TestCS.Event1Handler(getevent)" here.


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.
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

Sep 2 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Joe Campbell | last post: by
4 posts views Thread by Dave A | last post: by
2 posts views Thread by PiotrKolodziej | last post: by
4 posts views Thread by =?Utf-8?B?Y2FsZGVyYXJh?= | last post: by
15 posts views Thread by Bjoern Schliessmann | last post: by
reply views Thread by rosydwin | last post: by

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.