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

nested class events and scope question

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
3 1910
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
3
by: jena | last post by:
Hi I have code # BEGIN CODE def test(): def x(): print a a=2 # *** a=1
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
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...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
23
by: Steven D'Aprano | last post by:
I defined a nested function: def foo(): def bar(): return "bar" return "foo " + bar() which works. Knowing how Python loves namespaces, I thought I could do this:
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.