By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,691 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,691 IT Pros & Developers. It's quick & easy.

nested class events and scope question

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Carmella" <Ca******@anonymous.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #2

P: n/a
On 2005-03-24, Carmella <Ca******@anonymous.net> wrote:
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.
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

Nov 21 '05 #3

P: n/a
Thank you both for pointing out that I was trying to do this in the
constructor. Now it's working.
Mel
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.