472,364 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 1389
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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.