Frage deutsch
~~~~~~~~~~~~~
Wozu ist die Kombination DIM SHARED gut?
 

Question English
~~~~~~~~~~~~~
What's DIM SHARED good for?
 

Antwort 1
~~~~~~~~
[ von Thomas Antoni, 19.5.2002 - 2.7.2002 ]
.
Mit
DIM SHARED <Variablenname1> [AS <Typ> [,<Variablenname2> AS ...]]
DIM SHARED <Feldname> (<Anzahl Feldelemente%-1>)
 
kannst Du im Hauptprogramm eine Variable oder ein Feld definieren ("dimensionieren"), das auch von allen SUBs und FUNCTIONs aus zugreifbar ist und von diesen gelesen und geändert werden kann.
 
Beispiele:
DIM Egon&
' Difinition der 32-Bit-Integervariablen Egon&
DIM Anna AS LONG, Otto AS SINGLE
'Definition der 32-Bit Integerzahl Anna und
'der einfach langen Gleitpunktvariablen Otto
DIM SHARED player$(3)
'Feld mit den 4 Feldelementen player$(0)...(3)
 
(Auszug aus meinem QBasic-Kochbuch, das Du auf
http://www.qbasic.de in der Tutorial-Rubrik herunterladen kannst)
 

Answer 2
~~~~~~~~~~
[ von KeiProductions ]
 
*** Tutorial 7 - Arrays, DIM and SHARED
Welcome to KeiProductions 7th tutorial for beginners, Arrays, DIM and SHARED. This tutorial will build on what we learned last tutorial about SUBs. We will learn more about moving variables between SUBs, as well as explicitly creating new variables. So let's get started!
 
First of all, do this. Create a SUB with no parameters entitled PrintHi. Then put this in the SUB:
PRINT "Hi! ";
PRINT "The value of X is:" + str$(X)
 
Now in the main module, type this:
CLS: X = 2
PrintHi
 
Run the program. What happens? The screen goes blank (that's natural), and the words Hi! The value of X is 0 appear on your screen. What happened? The program is working fine, trust me. Try inputting any value for X. Nothing. It shows it as zero every time. That's where DIM SHARED comes in.
 
DIM SHARED lets you exchange values between any and all SUBs and the main module. Without it, a variable in the main module will only be accessible in the main module, and a variable in the SUB only accessible in the SUB. The format is simply put the variable name after DIM SHARED.
 
Back in our little program, add this code to the top of the main module:
DIM SHARED X
 
Now QBasic knows that X will be readable everywhere. Run the program, and there's your solution! X prints out fine.
 
But that's not all DIM does. DIM SHARED is just another function of DIM, and I'll explain what DIM does right now. DIM is used to basically declare variables before they are used. The program will be ready for them, in other words. But what's the point of this, you say. Well, normally you don't need to use it, and really, there's no point. But in some cases, there is! Here's where we introduce an array. An array is a variable that has several different values in it. Let's say, for example, that you want to list the 7 days of the week. You could do this:
Day1$ = "Monday"
Day2$ = "Tuesday"
Day3$ = "Wednesday"
etc...
 
But you'd need a lot of code to view a certain day (say the user inputs a number, and your program prints the day that number corresponds to.) Well, an array works like this:
Day$(1) = "Monday"
Day$(2) = "Tuesday"
Day$(3) = "Wednesday"
etc...
 
See, all the values are stored in Day$. To get an entry, just put down the corresponding entry. For example, Day$(2). So say you asked the user to input a number between 1 and 7. If the number was in the variable DayNum, and you needed to print out the day, you would just do this:
PRINT Day$(DayNum)
 
This obviously limits the code you need. Instead of multiple IF commands, you just put one PRINT command. Simple as that.
 
Now we're going to bring DIM back into the picture. I hope you're still understanding. Anyways, you're going to have to use DIM a lot with arrays, and here's the reason: Arrays with over 10 entries have to be DIMmed before they can be used, or you'll get an error! Arrays with under 11 entries will work fine, but for anything else, use DIM. Here's how you would write it out: DIM Array(Numberofentries). So that's basically what DIM does!
 
Well, in conclusion, in this tutorial we learned how to use arrays and store multiple entries under one variable or string. We also learned how to declare variables before they're used. And we learned how to share variables between SUBs. Have any questions? Any comments? Just mail me and I'll get right back to you!
This concludes the tutorial. Thank you.
 
 

Answer 3
~~~~~~~~~~~~~~~~
 
*** Question
My docs say something about local and global scope, but it's all kind of confusing. What's the real difference between local, STATIC, COMMON, SHARED, COMMON HARED, and all other flavors of variables?
 
*** Answer
Beginners with QuickBASIC sometimes have a hard time decrypting
all of the different types of variable scope. Microsoft hasn't
really helped anything with all the funny names for variable
scope. GLOBAL would have made more sense than SHARED for most.
Okay, let's look at how the QuickBASIC program is inevitably
structured:
 
1. First, there is the 'module' level. That is the
main part of the QuickBASIC program, the part where
execution starts, and most programmers declare their
constants, and put their main documentation.
2. Second, there is the SUB and FUNCTION level. Each
SUB and FUNCTION could be thought of as a miniprogram
unto itself. That's why SUBs are called that:
subprogram.
3. Third, if you write bigger programs, you may actually
have two or more modules, each one having its own
SUBs and FUNCTIONs.
 
Okay, then, any variable used at the modular level, or level 1, is
accessible, or in the 'scope' of the modular level. If there is
a variable called Foo at the modular level, with a value of 7, then
any Foo at the SUB or FUNCTION level could also be called Foo,
without interfering with the modular Foo. Think of each module
level variable and each SUB and FUNCTION variable as being on
different continents. They can have the same name with no problem.
 
But, suppose you want a SUB or FUNCTION to have access to the
Foo that was declared at the modular level. This is where the
SHARED declarator comes in. In the SUB somesubprog, to have
access to the Foo that was declared at the modular level, just
add the declaration:
 
SHARED Foo
 
Any SUB or FUNCTION that doesn't want to have access to the
modular Foo doesn't have to declare it as SHARED. This is a
powerful feature, once you get the hang of it and feel confident
enough to use it wisely.
 
Now, suppose that you want a number of your SUBs or FUNCTIONs to
have access to a common group of variables. At the modular
level, the declaration would be:
 
DIM SHARED Foo
 
This would give ALL of the SUBs and FUNCTIONs of a given module
access to the variable Foo. Any access of Foo at any level will
alter the global variable.
 
Now, suppose you have a multimodule program that has FIRST.BAS
and SECOND.BAS linked together. Suppose you want them to
communicate with one another via a common global variable. This
is where COMMON SHARED comes in.
 
Now that we've covered this, there is the issue of the STATIC
declarator. Normally, variables at the SUB and FUNCTION level
are dynamic, which means they disappear when the routine returns
to the place that it was called from. By declaring a variable
STATIC, we can be assured that whatever the variable's value was
when we left, it will be when we return. To declare only a few
of the variables as STATIC, use the form:
 
SUB FooSub ()
STATIC Variable1, Variable2, etc.
:
:
END SUB
 
But, if you want ALL the variables to be STATIC, use the following
method:
 
SUB FooSub () STATIC
:
:
:
END SUB
 
There are certain speed advantages to STATIC SUBs and FUNCTIONs,
since variables are not created on the stack, but that is a more
advanced issue.
 
 
So, in summary:
1. SHARED allows SUBs and FUNCTIONs to use modular variables,
2. COMMON allows modules to share variables between themselves,
3. STATIC allows variables to retain their value between
calls to the SUB or FUNCTION in question.
 
 

Answer 4
~~~~~~~~~~~~~~~
[ from the VBDOS FAQ at comp.lang.basic.visual, June 10, 1994 ]
 

How do I use (create) global variables in QuickBASIC and VBDOS?
 
VBDOS provides the user with two types of global variables.
These are both used in declarations of variables.
 
To share variables between all subs and functions in a specific
module, use the SHARED keyword. This makes that specific
variable global _in that module_. For example:
 
DIM SHARED CancelFlag AS INTEGER
 
would make the variable CancelFlag a global variable in that
module.
 
To share global variables between separate modules, use the
COMMON keyword. For example:
 
COMMON SHARED CancelFlag AS INTEGER
 
would make the variable global between all modules that this
common statement appears in, and since we are using the
SHARED keyword also, this will also be shared in all the subs
and functions in the modules which this declare statement
appears. All COMMON statements must be matched between
modules which the variables should be global in. For example,
if you have one set of 10 COMMON statements in one module,
and a different set of 10 COMMON statements in another
module in the same project, you will get a 'Type Mismatch
Error'. Make all COMMON blocks identical in all the modules
in a specific project.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

[ The QBasic-MonsterFAQ --- Start Page: www.antonis.de/faq ]