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

Variable scope in nested classes - can't get to my variable

in VB.NET I'm trying to access my variable without resorting to SHARED
scope.
Here's the situation: class MYLABEL inherits LABEL and has an additional
property called MYVAR. In MYLABEL I set an ADDHANDLER for ME.MOUSEDOWN event
to ME.MYHANDLER. PRIVATE SUB MYHANDLER (part of the MYLABEL class)
creates
a new context menu at runtime and does an ADDHANDLER for the context menu
item CLICK event to MYSUB. PUBLIC SUB MYSUB then creates an instance of a
new form (MYFORM) and does a MYFORM.SHOWDIALOG. In the MYFORM class I have
a button and in that buttons click event I need to access the variable MYVAR
(which is a property in the MYLABEL class).

Now, I know this description sounds complicated but it's not really. Here's
what happens. The user right clicks on a label and a context menu appears.
User clicks on a context menu choice which causes a new form (MYFORM) to
appear. Within the MYFORM the user enters data into a textbox, hits SAVE
button and that info needs to be transfered into the MYVAR variable (which
is
a property of MYLABEL).

My problem is that I can't get to the MYVAR variable. If it were two forms
I'd just pass a formname as a parameter to a procedure call , but because
there's a mousedown event in the middle (which has a specific signature for
parameters), I'm at a loss as to how to do this. Any help greatly
appreciated.
Nov 21 '05 #1
5 1446
You forgot to mention the scope of your MyVar property currently , what is
it: public, private, protected, etc...?

"JohnR" wrote:
in VB.NET I'm trying to access my variable without resorting to SHARED
scope.
Here's the situation: class MYLABEL inherits LABEL and has an additional
property called MYVAR. In MYLABEL I set an ADDHANDLER for ME.MOUSEDOWN event
to ME.MYHANDLER. PRIVATE SUB MYHANDLER (part of the MYLABEL class)
creates
a new context menu at runtime and does an ADDHANDLER for the context menu
item CLICK event to MYSUB. PUBLIC SUB MYSUB then creates an instance of a
new form (MYFORM) and does a MYFORM.SHOWDIALOG. In the MYFORM class I have
a button and in that buttons click event I need to access the variable MYVAR
(which is a property in the MYLABEL class).

Now, I know this description sounds complicated but it's not really. Here's
what happens. The user right clicks on a label and a context menu appears.
User clicks on a context menu choice which causes a new form (MYFORM) to
appear. Within the MYFORM the user enters data into a textbox, hits SAVE
button and that info needs to be transfered into the MYVAR variable (which
is
a property of MYLABEL).

My problem is that I can't get to the MYVAR variable. If it were two forms
I'd just pass a formname as a parameter to a procedure call , but because
there's a mousedown event in the middle (which has a specific signature for
parameters), I'm at a loss as to how to do this. Any help greatly
appreciated.

Nov 21 '05 #2
John,

I found it with user controls always difficult to see how the user made it.
Can you describe that a little bit, because in my opinion is the problem to
get to that MYVAR variable in that userControl MYLABEL.

To be more precise in my question, did you directly inherited "Label" or did
you drag a label on a usercontrol?

Cor
Nov 21 '05 #3
Here's a summary of the code:

public class MYCLASS
inherits label
private MYVAR
addhandler me.MOUSEDOWN, addressof ME.MYHANDLER

private sub MYHANDLER(takes the parametes of mouseeventargs, so I can't
pass MYVAR as a param... any way around that?)
--creates context menu at runtime which when clicked on calls MYSUB

public sub MYSUB
--creates runtime instance of a form MYFORM
MYFORM.SHOW

The MYFORM form has a button, and in that buttons click event, I need to
access MYVAR
Additional info:
I originally had the MYFORM class defn in a different .vb file. When I
created a wrapper module and stuck everything in there, and moved MYVAR
outside of the procs with a FRIEND MYVAR AS STRING then I succeeded in
accessing the variable. However when it's in the wrapper module, the
windows designer doesn't see the MYFORM as something it can edit. So it
looks like I need to move the MYFORM code to a seperate file when I want to
edit it in windows designer, then move it back to the wrapper module when it
runs... Any other suggestions as to how to do this better?
Nov 21 '05 #4
Declare MyVar as public in your class.

"JohnR" wrote:
Here's a summary of the code:

public class MYCLASS
inherits label
private MYVAR
addhandler me.MOUSEDOWN, addressof ME.MYHANDLER

private sub MYHANDLER(takes the parametes of mouseeventargs, so I can't
pass MYVAR as a param... any way around that?)
--creates context menu at runtime which when clicked on calls MYSUB

public sub MYSUB
--creates runtime instance of a form MYFORM
MYFORM.SHOW

The MYFORM form has a button, and in that buttons click event, I need to
access MYVAR
Additional info:
I originally had the MYFORM class defn in a different .vb file. When I
created a wrapper module and stuck everything in there, and moved MYVAR
outside of the procs with a FRIEND MYVAR AS STRING then I succeeded in
accessing the variable. However when it's in the wrapper module, the
windows designer doesn't see the MYFORM as something it can edit. So it
looks like I need to move the MYFORM code to a seperate file when I want to
edit it in windows designer, then move it back to the wrapper module when it
runs... Any other suggestions as to how to do this better?

Nov 21 '05 #5
Thanks for the quick reply...

I did think about declaring MYVAR as public, but on a "elegance" level I
hate declaring vars as public... it was always drilled into me that in
writing bulletproof code you aways strive for the smallest variable scope
possible, using public or global vars only when absolutely required. On a
more realistic level, I'm going to be having lots of instances of MYCLASS
floating around (possibly around 20-30) at the same time, so if I declare it
as public, won't the MYVAR variables be stepping on each other?

I guess the bottom line is that the "real" way to see MYVAR is to pass it
along as a parameter. So given the fact that one of the links in the chain
of procedures that are being called is a MouseDown event (which has a
predefined set of parameters), the question becomes this: Is there any way
to modify the parameter signature of the MouseDown event. Is there some
kind of re-definition or inheritance that you can use on a mouse event
(similar to the way you can inherit and extend any other control class)?
"Alien2_51" <da***************@monacocoach.removeme.com> wrote in message
news:2B**********************************@microsof t.com...
Declare MyVar as public in your class.

"JohnR" wrote:
Here's a summary of the code:

public class MYCLASS
inherits label
private MYVAR
addhandler me.MOUSEDOWN, addressof ME.MYHANDLER

private sub MYHANDLER(takes the parametes of mouseeventargs, so I
can't
pass MYVAR as a param... any way around that?)
--creates context menu at runtime which when clicked on calls
MYSUB

public sub MYSUB
--creates runtime instance of a form MYFORM
MYFORM.SHOW

The MYFORM form has a button, and in that buttons click event, I need to
access MYVAR
Additional info:
I originally had the MYFORM class defn in a different .vb file. When I
created a wrapper module and stuck everything in there, and moved MYVAR
outside of the procs with a FRIEND MYVAR AS STRING then I succeeded in
accessing the variable. However when it's in the wrapper module, the
windows designer doesn't see the MYFORM as something it can edit. So it
looks like I need to move the MYFORM code to a seperate file when I want
to
edit it in windows designer, then move it back to the wrapper module when
it
runs... Any other suggestions as to how to do this better?

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
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:...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
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...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.