473,721 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1667
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.P age
Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) 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******** *************** ***********@mic rosoft.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******** *************** ***********@mic rosoft.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.P age
Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) 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.P age
{
private TestClass oTest;

protected void Page_Load(objec t sender, EventArgs e)
{
oTest = new TestClass();
oTest.Event1 += new Event1Handler(g etevent);
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(o bject 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(g etevent);

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.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;

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

protected void Page_Load(objec t sender, EventArgs e)
{
oTest = new TestCS();
oTest.Event1 += new Event1Handler(g etevent);
oTest.getEvent( );
}

private void getevent(object sender, Event1EventArgs e)
{
Label1.Text = e.s;
}
}
My Class
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;

public class TestCS
{
public delegate void Event1Handler(o bject 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.o rgwrote
in message news:ee******** ******@TK2MSFTN GP06.phx.gbl...
"ThatsIT.net.au " <me@workwrote in message
news:15******** *************** ***********@mic rosoft.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.P age
Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventAr gs) 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.P age
{
private TestClass oTest;

protected void Page_Load(objec t sender, EventArgs e)
{
oTest = new TestClass();
oTest.Event1 += new Event1Handler(g etevent);
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(o bject 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******** *************** ***********@mic rosoft.com...
Thank very much, I'm almost there but im getting an error on this line.
any ideas?

oTest.Event1 += new Event1Handler(g etevent);

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.Event1Ha ndler(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(g etevent);
with
oTest.Event1 += getevent;

"ThatsIT.net.au " <me@workwrote in message
news:0B******** *************** ***********@mic rosoft.com...
Thank very much, I'm almost there but im getting an error on this line.
any ideas?

oTest.Event1 += new Event1Handler(g etevent);

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.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;

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

protected void Page_Load(objec t sender, EventArgs e)
{
oTest = new TestCS();
oTest.Event1 += new Event1Handler(g etevent);
oTest.getEvent( );
}

private void getevent(object sender, Event1EventArgs e)
{
Label1.Text = e.s;
}
}
My Class
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;

public class TestCS
{
public delegate void Event1Handler(o bject 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.o rgwrote
in message news:ee******** ******@TK2MSFTN GP06.phx.gbl...
>"ThatsIT.net.a u" <me@workwrote in message
news:15******* *************** ************@mi crosoft.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.P age
Dim WithEvents oTest As TestClass = New TestClass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventA rgs) 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.P age
{
private TestClass oTest;

protected void Page_Load(objec t sender, EventArgs e)
{
oTest = new TestClass();
oTest.Event1 += new Event1Handler(g etevent);
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(o bject 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.c omwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
"ThatsIT.net.au " <me@workwrote in message
news:0B******** *************** ***********@mic rosoft.com...
>Thank very much, I'm almost there but im getting an error on this line.
any ideas?

oTest.Event1 += new Event1Handler(g etevent);

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.Event1Ha ndler(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******** *************** ***********@mic rosoft.com...
solved it
replaced
oTest.Event1 += new Event1Handler(g etevent);
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******** *************** ***********@mic rosoft.com...
>You need to fully qualify nested types - so it should be "new
TestCS.Event1H andler(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.Even t1 +=" 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.o rgwrote
in message news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
"ThatsIT.net.au " <me@workwrote in message
news:10******** *************** ***********@mic rosoft.com...
>>You need to fully qualify nested types - so it should be "new
TestCS.Event1 Handler(geteven t)" 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.Even t1 +=" 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.Event1Ha ndler(oTest_Eve nt1);

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
1859
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 array returned by ButtonClicked.GetInvocationList method event that is raising an exception. Presently i am enclosing in try catch block and would like to avoid that. Any suggestions on how to find if any body is hooked/listening to this event.
0
1449
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 (0x80041003): Exception from HRESULT: 0x80041003. at Microsoft.EnterpriseInstrumentation.EventSinks.WmiEventSink ..Write(Object eventToRaise) at
4
1593
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 the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). The thing that was not explained is why should I need to cast the sender to 'object' when I know...
2
1695
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 the parent form. This is fine. The problem occurs when I want to raise an event for a user clicking on one of the clsRecords which are on the grid. So I've placed: Public Event GridRecordClicked(ByVal rec As clsGridRecord,
2
1081
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 yet. RS port is receiving very fast the portions of data. When first data receives, user is asked to do something with this data. Then each another raise of this event is dooing the same what user did with this data.
4
1541
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. In the constructor, I check the to see if the employees birthday is today. If it is today, then I raise an event.
4
4640
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 in that language. So now I am fighting with so simple thing like raising a simple event as follow, and have a complie error, with no damn idea what this compiler want. The code is as follow
15
4574
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 elements need to be (with some level of abstraction to my specific problem) - sources (here begin currents) - ground (here end currents)
0
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.