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

avoiding global variables

Dan
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
Jul 17 '05 #1
8 7636
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

Jul 17 '05 #2
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.
Jul 17 '05 #3

"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

Jul 17 '05 #4
Dan
"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
Jul 17 '05 #5
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
Jul 17 '05 #6

"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
Jul 17 '05 #7
Dan
"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
Jul 17 '05 #8

"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
Jul 17 '05 #9

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
6
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...

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.