Qbasicnews.com

Full Version: how do you use DIM?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
can anyone explain to me how DIM statement works? :bounce:
:rotfl: :rotfl: :bounce: :bounce: :barf:
See the QB online help:

http://qbasicnews.com/qboho/qckdim.shtml

Also, please avoid using excessive emoticons when they don't serve any meaningful purpose... see the FAQ.
ok, can you give me an example?
Uhm... check the Example link on that page... http://qbasicnews.com/qboho/qckadvr.dimx.shtml
Quote:can anyone explain to me how DIM statement works? :bounce:
:rotfl: :rotfl: :bounce: :bounce: :barf:
Example of excessive use of emoticons ^^ :lol:
hehe thank you :bounce:
I'll give you a brief definition of DIM.

The most common usage of DIM is to dimension an array, for example: DIM KEYARRAY (0 TO 99)
This sets up an array called KEYARRAY of 100 elements. Note that each element can be integer, single, long or double based on the default variable type at the time. If you didn't set up a default, then it will be single.

By setting it up I mean that elements 0 to 99 will be allowed to be referenced. If you reference KEYARRAY(100), for example, you'll get a runtime error.

The example sets up a one demensional array. You can set up 2 and more dimensional arrays.
Example: ZARRAY (1 TO 999, 0 TO 9)
Each of the 1000 elements in the first dimemsion can have 10 subordinate elements.

The DIM for an array also lets you specify the type of variable for the array.
Example: BIGNUMS (1 TO 100) AS DOUBLE

DIM is also used to explicitly define the type of a variable regardless of what the default is currently.
Example: DIM LARGENUM AS DOUBLE
When you later use LARGENUM, it will be treated as double.

Another use is for declaring a variable as SHARED, that is, that it can be used by any module in the program, just like CONST constants are.
Example: DIM SHARED ERRORCODE

That's about the extent of my DIM knowledge.
*****
If you use arrays, at first it may look more convient to start them with No. 1, but later you will find that No. 0 is much better. You will find out on your own why, i just told it so that you might start getting used to that. At programmers everything starts with 0 Smile

Dim works this way too:

DIM array(99) AS INTEGER
(or DIM array%(99) - with Basic i found this second form better later since you will always know the variable's type).

That above is equal to DIM array(0 TO 99) by default (It is possible to change it, but as i said: get used to that 0 sooner, and you will get less annoyed with your early codings later).
Quote:If you use arrays, at first it may look more convient to start them with No. 1, but later you will find that No. 0 is much better......

DIM array(99) AS INTEGER

That above is equal to DIM array(0 TO 99) by default (It is possible to change it, but as i said: get used to that 0 sooner, and you will get less annoyed with your early codings later).

What you say is true. However, I suggest that the possible zero or one confusion can be avoided by ALWAYS declaring the lower limit of the array EXPLICITLY, like:
DIM array (0 to 99)
or
DIM monthtable (1 to 12)

Although the default lower limit is zero, in the above monthtable (1 to 12) we are defining that the only valid indexes into this table are from 1 to 12. If we just said monthtable (12), it would probably still work fine, except in the case where by error we used and index of zero, which would run, but give bad results. If we used an index of zero for the (1 to 12) case, we would get an out of range error and become aware of the error immediately.
*****
Starting at index 0 is faster
Pages: 1 2