473,396 Members | 1,734 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,396 software developers and data experts.

Raising Event

I have an event declared in class1

Now i want to raise the event in the my class2 or anywhere other than class1

Is it possible? If so how

Thank you
Jul 21 '05 #1
5 1544

"Santhoshi" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
I have an event declared in class1.

Now i want to raise the event in the my class2 or anywhere other than
class1.

Is it possible? If so how?

Basically you will have to declare a method on class1 that can raise the
event. Thank you

Jul 21 '05 #2
Cor
Hi Santhosi,

You have to declare your event "public"

I hope this helps?

Cor
Jul 21 '05 #3

"Cor" <no*@non.com> wrote in message
news:OX******************@TK2MSFTNGP10.phx.gbl...
Hi Santhosi,

You have to declare your event "public"

I hope this helps?
Public is actually irrelevent. The event keyword(in C# anyway, don't see VB
being that different) is specifically used over a plain delegate type
because it restricts outside classes from raising or clearing the event,
otherwise a plain delegate would suffice. This creates an event in the
assembly, this event has two required methods (add and remove in C#, .addon
and .removeon in IL) that performs the addition and removal of a delegate
from the event. Even when the event is declared as a field underneath it is
a property with a backing delegate field(compiler handles that). Because of
this, an event type *never* directly provides access to the backing
delegate, in fact code has no way to actually get to the backing delegate
because of the nature of events.

Now, there is a raise accessor defined by the CLI spec, specifically .fire
in IL, but the recommendation is for that accessor to be protected and it
isn't surfaced in C# and VB(AFAIK). This accessor doesn't have a restriction
that it *must* be protected, to my knowledge, and if you use a language that
allows public raise accessors, it will be possible to raise the event from
another class, otherwise you are basically out of luck(note, C# doesn't
consume raise accessors in my experience).

It really is simplier to simply write a method to do what you want(thats
basically all a raise accessor is, after all).
Cor

Jul 21 '05 #4
Cor
Hi Daniel,

Can you try this (needs only to open the ide, start a VB project and paste
it in, and set the start in your properties to sub main), and then give
comments, I did it in VB, because there was my answer based on.

This was the question
I have an event declared in class1.
Now i want to raise the event in the my class2 or anywhere other than
class1.
Is it possible? If so how?
This was my answer
You have to declare your event "public"

I am curious to your answer

Cor

This is my sample.
\\\
Public Class class1
Public Event Hello()
Public Sub sayhello()
RaiseEvent Hello()
End Sub
End Clas
Public Module class2
Private WithEvents myhi As New class1
Public Sub Main()
myhi.sayhello()
End Sub
Private Sub hihi() Handles myhi.Hello
MessageBox.Show("Hello Daniel")
End Sub
End Module
///
Jul 21 '05 #5
Cor <no*@non.com> wrote:
Can you try this (needs only to open the ide, start a VB project and paste
it in, and set the start in your properties to sub main), and then give
comments, I did it in VB, because there was my answer based on.

This was the question
I have an event declared in class1.
Now i want to raise the event in the my class2 or anywhere other than
class1.
Is it possible? If so how?
This was my answer
You have to declare your event "public"


You don't have to declare the event public in order to *raise* the
event. You have to declare the event public in order to *subscribe* to
(or unsubscribe from) it.

The reason your code allows other classes to raise the events is
because you've *also* given a sayhello method. It would be possible to
give that method without the Hello event being public, in which case
only the class could subscribe to the event, but anyone could raise it
(indirectly, via the method call).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

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...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
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...
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....
2
by: =?Utf-8?B?Um9i?= | last post by:
I'm new to event processing in vs 2005 - what I'm trying to achieve is broadly a network simulation of two workstations sending packets to each other . I have defined a class 'Workstation' and I...
7
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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.