473,395 Members | 1,972 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,395 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 1655
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rajesh.V | last post by:
I have made a custom control in asp.net using webcontrols, and am raising my event. I find that if no control is hooked into the event it goes for a toss. I tried to find length of the delegate...
0
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
2
by: PiotrKolodziej | last post by:
Hi I have an event that receives data from RS port rs.DataReceived += new SerialDataReceivedEventHandler(rs_DataReceived); I need to block raising an event when the previous one has not lelft...
4
by: sloan | last post by:
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....
4
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, Until now I was programing for more that 6 years using VB.net simply because I come from that word and used to. Now I need to switch to C# simply because my customer want its project...
15
by: Bjoern Schliessmann | last post by:
Hello all, I'm trying to simulate simple electric logic (asynchronous) circuits. By "simple" I mean that I only want to know if I have "current" or "no current" (it's quite digital) and the only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.