Raising Events Between Classes

M

MeltingPoint

Just wondering if this is the right way to do things, or if there's a
better way.

<code>
Public Class UserClass //This is the class I want the user to use:)//
Event UserEvent(ByVal s As String)

Public Sub DoSomething()
Dim pc as New MyFriendClass
AddHandler pc.MyFriendEvent, AddressOf Me.RaiseUserEvent
pc.WorkAway
End Sub

Public Sub RaiseUserEvent(ByVal s As String)
RaiseEvent UserEvent(s)
End Sub
End Class

Friend Class MyPrivateClass
Event MyPrivateEvent(ByVal s As String)

Public Sub WorkAway()
...
RaiseEvent MyPrivateEvent(s)
End Sub
End Class
</code>

I hope this code makes sense to you, and if it does, is it right? Is
there a better way to raise events between classes?

Thanks for your time,
MP
 
M

MeltingPoint

Meltingpoint,


Probably are you looking exact as you say it

Raisevent.

Have a look at the sample at the bottom of this page and not in the
top.
http://msdn.microsoft.com/library/default.asp?url=/library/enus/vblr7/
html/vastmraiseevent.asp

I hope this helps?

Cor

Thanks Cor, I've seen you answer many peoples questions in this group
and I thank you for taking time to answer mine. I have spent 100's of
hours in MSDN as I am still learning, and have been to every page with
the word 'event' in it. At some point I remember coming across the
statment 'classes cannot raise events in other classes' so the code I
wrote previously would be cosidered a workaround, which I don't like.
The exapmle you gave, although close, is just a single class raising an
event, where I'm looking for something like: ClassLibrary(PrvateClass2
--> RaiseEvent --> PublicClass1 --> RaiseEvent -->) EndUser Handle Event

Thanks again.
MP
 
C

Cor Ligthert

MP,

Maybe I don't understand you. In the sample at the bottom is of the page is
a sample where is created a class that raises an event.

That class is used (as object) in another class (the form what is an object
as well, however in VBNet is a kind of automatic instancing feature for the
main form class).

ClassLibrary(PrvateClass2 --> RaiseEvent --> PublicClass1 -->
RaiseEvent -->) EndUser Handle Event

This looks strange for me by the way, I can understand it, when it is

ClassLibrary(PublicClass2--->RaiseEventA--->Instanced Class1
(object) --->RaiseEventyY ---) End user Handle EventA from instanced Class2

What do I miss in your question?

Cor
 
G

Guest

I believe the modified code below will work:

Public Class UserClass //This is the class I want the user to use:)//
Public Event UserEvent(ByVal s As String)
Private pc as MyFriendClass = New MyFriendClass

Public Sub RaiseUserEvent(ByVal s As String) handles pc.MyPrivateEvent
RaiseEvent UserEvent(s)
End Sub
End Class

Friend Class MyPrivateClass
Event MyPrivateEvent(ByVal s As String)

Public Sub WorkAway()
...
RaiseEvent MyPrivateEvent(s)
End Sub
End Class
 
C

Cor Ligthert

doh,

Now by the message from Dennis I see it.
I was probably fixed by that "add handler" and could not think why it was a
problem.

How would you do it in another way?

Sorry for my misunderstanding.

Cor
 
M

MeltingPoint

I believe the modified code below will work:

Public Class UserClass //This is the class I want the user to use:)//
Public Event UserEvent(ByVal s As String)
Private pc as MyFriendClass = New MyFriendClass

Public Sub RaiseUserEvent(ByVal s As String) handles
pc.MyPrivateEvent
RaiseEvent UserEvent(s)
End Sub
End Class

Friend Class MyPrivateClass
Event MyPrivateEvent(ByVal s As String)

Public Sub WorkAway()
...
RaiseEvent MyPrivateEvent(s)
End Sub
End Class

Yup, your code works as well, except "pc" will be instanced more than
once in my code, which is why I use the addhandeler() for each instance
of "pc". Sorry to tie your brains in a knot :) but I wasn't asking how
to do it, I was asking if the coding convention of passing events
through multiple classes to the user was the best way to do it. To state
it as clearly as possible, in my private class "pc" I just wanted to
call "RaiseEvent UserClass.UserEvent" bypassing the need for
addhandlers. I was thinking that there was a way to use delegates, and I
guess theres not.

OK Here is the thought process for what I'm doing:

1) the user creates an instance of UserClass (Dim WITHEVENTS UserClass)
2) multiple DoSomething() calls to the class create a number of "pc's"
3) as each pc finishes the user should be notified, but since pc was not
instanced by the user, pc can't directly notify the user.
###NONE of the above is in question###
My question (i guess) is; is there a way to hard code each pc with the
address of a sub in UserClass which will in turn raise the event to the
user, therefor bypassing the need to use addhandler in the UserClass.

Thanks again for your time
MP
 
G

Guest

I"ve not done it before but I believe you can set up a delegate in your pc
class then in the pc New sub, pass a parameter of the address of the
procedure you want to handle the pc events when you instantiate each pc
class. You might have the procedure handling the events scoped as either
public or maybe friend.
 

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