473,574 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SHOWDIAL OG. 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 1459
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.SHOWDIAL OG. 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******** *************** ***********@mic rosoft.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
4319
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 don't like the brackets.
6
2560
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: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the...
6
3181
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
8551
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
1921
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, in ? A() File "x1x2.py", line 11, in A
11
3012
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
25644
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that...
3
2114
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 decided to use nested class because I realized that it was the most convenient way to implement my need. Since this feature is supported in many...
0
143
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 now, even with the pickle limitation (not every class are intended to be pickled), more you didn't give any evidence or any pertinent quote of this...
0
7814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7736
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8067
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5307
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2252
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1071
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.