473,803 Members | 4,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7653
Have you tried making the variable STATIC ?

"Dan" <a@a.com> wrote in message
news:IN******** ***********@twi ster.nyroc.rr.c om...
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******** ***********@twi ster.nyroc.rr.c om...

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******** ************@co mcast.com...

"Dan" <a@a.com> wrote in message
news:IN******** ***********@twi ster.nyroc.rr.c om...

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.MouseIsDown Flag Then
Jul 17 '05 #6

"Dan" <a@a.com> skrev i en meddelelse
news:3O******** ***********@twi ster.nyroc.rr.c om...
"Steve Gerrard" <no************ *@comcast.net> wrote in message
news:ze******** ************@co mcast.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.te le.dk...

"Dan" <a@a.com> skrev i en meddelelse
news:3O******** ***********@twi ster.nyroc.rr.c om...
"Steve Gerrard" <no************ *@comcast.net> wrote in message
news:ze******** ************@co mcast.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******** ***********@twi ster.nyroc.rr.c om...
"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
17887
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 overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
4
24189
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(); function loadUrlVariables() { varString = location.search;
12
5889
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 these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding the convenience and safety), as this is such a fundamental thing, I believe most of you have a say...
17
5635
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3058
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
9
8663
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11832
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 $MYVAR, $a; ?>
6
1633
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 including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is there a way to make the variables unmodifiable in config.php so that scripts that use them ,
112
5492
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 that may print some messages. foo(...) { if (!silent)
0
9562
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4274
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
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.