Quick question about passing variables to subs to reduce the need for
publicly declared variables in VB6. If I have an event sub (MouseDown) that
runs a few lines of code, how can I use a variable in that sub without
making it global? I tried adding the variable to the arguments of the sub,
but VB doesn't seem to like anything other than the predescribed format of
the event. So, for example, given the MouseDown event:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single,
Y As Single)
I would intuitively add a variable in such a manner:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single,
Y As Single, foo as Whatever)
which doesn't work. How should I do this instead?
-Dan 8 7448
Have you tried making the variable STATIC ?
"Dan" <a@a.com> wrote in message
news:IN*******************@twister.nyroc.rr.com... Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown)
that runs a few lines of code, how can I use a variable in that sub without making it global? I tried adding the variable to the arguments of the
sub, but VB doesn't seem to like anything other than the predescribed format of the event. So, for example, given the MouseDown event:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
I would intuitively add a variable in such a manner:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single, foo as Whatever)
which doesn't work. How should I do this instead?
-Dan
On Fri, 20 Feb 2004 21:15:20 GMT, Dan wrote: Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown) that runs a few lines of code, how can I use a variable in that sub without making it global? I tried adding the variable to the arguments of the sub, but VB doesn't seem to like anything other than the predescribed format of the event. So, for example, given the MouseDown event:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
I would intuitively add a variable in such a manner:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single, foo as Whatever)
which doesn't work. How should I do this instead?
Have your MouseDown event call a separate sub and pass the args to that.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
mySub Button, Shift, X, Y, foo
End Sub
....and then mySub would do the actual work, and could be called from
somewhere else directly.
--
auric "underscore" "underscore" "at" hotmail "dot" com
*****
The computer industry did not get where it is today by fixing its
problems.
"Dan" <a@a.com> wrote in message
news:IN*******************@twister.nyroc.rr.com... I would intuitively add a variable in such a manner:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single, foo as Whatever)
which doesn't work. How should I do this instead?
-Dan
Since the mouse down event is called by VB, it wouldn't know what to
pass for foo anyway.
Is this what you mean?
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
Single,
Y As Single)
Dim Foo as Variant
'use Foo here
End Sub
"Steve Gerrard" <no*************@comcast.net> wrote in message
news:ze********************@comcast.com... "Dan" <a@a.com> wrote in message news:IN*******************@twister.nyroc.rr.com... I would intuitively add a variable in such a manner:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single, foo as Whatever)
which doesn't work. How should I do this instead?
-Dan
Since the mouse down event is called by VB, it wouldn't know what to pass for foo anyway.
Is this what you mean?
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim Foo as Variant 'use Foo here End Sub
I should have been a little more explicit in my question. Let me use an
example. So I have the variable foo that I'm using in various parts of the
program. Now, when the user presses a mouse button, the code inside the
MouseDown event needs to know what foo is. But, foo is not specific just to
the MouseDown event. There are other subs and funtions that also need to
know what foo is. So making foo STATIC would not solve the problem.
-Dan
On Sat, 21 Feb 2004 05:13:35 GMT, "Dan" <a@a.com> wrote:
<snip> I should have been a little more explicit in my question. Let me use an example. So I have the variable foo that I'm using in various parts of the program. Now, when the user presses a mouse button, the code inside the MouseDown event needs to know what foo is. But, foo is not specific just to the MouseDown event. There are other subs and funtions that also need to know what foo is. So making foo STATIC would not solve the problem.
Personally I use a Form/Module level UDT for such variables
Private Type TCMN
MouseIsDownFlag As Boolean
....
End Type
Private cmn As TCMN
......
If cmn.MouseIsDownFlag Then
"Dan" <a@a.com> skrev i en meddelelse
news:3O*******************@twister.nyroc.rr.com... "Steve Gerrard" <no*************@comcast.net> wrote in message news:ze********************@comcast.com...
Since the mouse down event is called by VB, it wouldn't know
what to pass for foo anyway.
Is this what you mean?
Private Sub Form_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single) Dim Foo as Variant 'use Foo here End Sub
I should have been a little more explicit in my question. Let
me use an example. So I have the variable foo that I'm using in various
parts of the program. Now, when the user presses a mouse button, the code
inside the MouseDown event needs to know what foo is. But, foo is not
specific just to the MouseDown event. There are other subs and funtions that
also need to know what foo is. So making foo STATIC would not solve the
problem.
To make it more elegant I would make a module called Global. In
that module I would place the public variables _and_ the
procedures that use them !
In the form events I would then call those global procedures and
not play with the global variables directly. I.e. define your
global variable and a few procedures/functions to work on them.
You could be extreme and place the variables in a class and make a
single public instantiated variable of that class and access the
variables through properties...
--
/\ preben nielsen
\/\ pr**@post.tele.dk
"preben nielsen" <pr**@post.tele.dk> wrote in message
news:40**********************@dread14.news.tele.dk ... "Dan" <a@a.com> skrev i en meddelelse news:3O*******************@twister.nyroc.rr.com... "Steve Gerrard" <no*************@comcast.net> wrote in message news:ze********************@comcast.com...
Since the mouse down event is called by VB, it wouldn't know what to pass for foo anyway.
Is this what you mean?
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim Foo as Variant 'use Foo here End Sub
I should have been a little more explicit in my question. Let
me use an example. So I have the variable foo that I'm using in various parts of the program. Now, when the user presses a mouse button, the code inside the MouseDown event needs to know what foo is. But, foo is not specific just to the MouseDown event. There are other subs and funtions that also need to know what foo is. So making foo STATIC would not solve the problem.
To make it more elegant I would make a module called Global. In that module I would place the public variables _and_ the procedures that use them !
In the form events I would then call those global procedures and not play with the global variables directly. I.e. define your global variable and a few procedures/functions to work on them. You could be extreme and place the variables in a class and make a single public instantiated variable of that class and access the variables through properties...
-- /\ preben nielsen \/\ pr**@post.tele.dk
Which is similar to what I do now. But I hear lots of shouting and fussing
about avoiding Public variables entirely and that people have spent their
entire programming career without using a single Public/Global variable. As
a QBasic native, that was not an impossible feat (liberally passing
variables amongst functions was elementary), but I still don't see how it's
possible in VB.
-Dan
"Dan" <a@a.com> wrote in message
news:5u*******************@twister.nyroc.rr.com... "preben nielsen" <pr**@post.tele.dk> wrote in message Which is similar to what I do now. But I hear lots of shouting and
fussing about avoiding Public variables entirely and that people have spent
their entire programming career without using a single Public/Global
variable. As a QBasic native, that was not an impossible feat (liberally passing variables amongst functions was elementary), but I still don't see how
it's possible in VB.
Either of these would work, and are essentially equivalent:
'---------
'Module1
Private mMyLong As Long
'use function and sub
Public Function GetMyLong() As Long
GetMyLong = mMyLong
End Function
Public Sub LetMyLong(ByVal NewVal As Long)
mMyLong = NewVal
End Sub
'or use property get/let
Public Property Get MyLong() As Long
MyLong = mMyLong
End Property
Public Property Let MyLong(ByVal RHS As Long)
mMyLong = RHS
End Property
'The calls look like this:
'-------
'Form1
Private Sub Command1_Click()
'sub/function
LetMyLongBe 3
MsgBox GetMyLong
End Sub
Private Sub Command2_Click()
'property let/get
MyLong = 2
MsgBox MyLong
End Sub This discussion thread is closed Replies have been disabled for this discussion. Similar topics
10 posts
views
Thread by Matt |
last post: by
|
4 posts
views
Thread by Andrew V. Romero |
last post: by
|
12 posts
views
Thread by David WOO |
last post: by
|
17 posts
views
Thread by MLH |
last post: by
|
33 posts
views
Thread by MLH |
last post: by
|
9 posts
views
Thread by CDMAPoster |
last post: by
|
5 posts
views
Thread by Sandman |
last post: by
|
6 posts
views
Thread by lazy |
last post: by
|
112 posts
views
Thread by istillshine |
last post: by
| | | | | | | | | | |