nested class events and scope question

C

Carmella

Is it possible for a class to create an instance of a private nested class
which raises events and then listen for those events? In the example below,
when InnerClass sings, OuterClass should say Bravo . I am not sure about the
scoping and I cannot get this to work.
Thanks
Mel

Public Sub OuterClass
Public Event Bravo()
Protected WithEvents MyInnerClass as InnerClass

Public Sub New
MyInnerClass = New InnerClass
End Sub

Private Sub HearSinging() Handles MyInnerClass.Sing
RaiseEvent Bravo()
End Sub

Private InnerClass
Friend Event Sing()

Public Sub New()
SingSomething()
End Sub

Private Sub SingSomething()
RaiseEvent Sing()
End Sub
End Class


End Class
 
S

smith

I may be wrong but try making the SingSomething sub public and calling it
from the outerclass *after* the inner class's constructor is done. Far as
I've seen it only shared events will fire from within a constructor because
essentially until the constructor is fully completed there is no object and
so the events aren't fully wired up.

Robert Smith
Kirkland, WA
www.smithvoice.com
 
D

David

Is it possible for a class to create an instance of a private nested class
which raises events and then listen for those events? In the example below,
when InnerClass sings, OuterClass should say Bravo . I am not sure about the
scoping and I cannot get this to work.

there's a few problems here, but none of them are scoping issues.

1. Probably a minor thing, since it might just be the nature of this
snippet, but nobody is catching the Bravo event.

2. You can't raise events from a constructor. The problem is that the
caller has not had time to call AddHandler on you, so nobody is set to
catch the event. VB.net kinda hides what's really going on from you,
so it's a bit confusing.

Dim s As New InnerClass
AddHandler s.Sing, AddressOf HearSinging

You see why that won't work, right? That's basically what's going on
here.
 
C

Carmella

Thank you both for pointing out that I was trying to do this in the
constructor. Now it's working.
Mel
 

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

Similar Threads

Help with Event handler, withevents, raise events 14
Raise event problem 1
Events 4
Events 3
How to expose vb.net events to Vb6? 1
RaiseEvent not firing... 10
Public Events 3
shared events possible? 3

Top