nested class events and scope question

  • Thread starter Thread starter Carmella
  • Start date Start date
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
 
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
 
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.
 
Thank you both for pointing out that I was trying to do this in the
constructor. Now it's working.
Mel
 
Back
Top