|
Dim | |||||||||||||||||||
Dim Variable / Array(size) as DataType. | |||||||||||||||||||
Parameters: NONE | |||||||||||||||||||
Returns: NONE | |||||||||||||||||||
Dim (dimension) is a multi purpose command. It allows us to declare Variables, Pointers, Arrays and Types. Note: This page is a of summary of sort, covering various common types of declarations ranging from Variables, Arrays, Types & Pointers. If you're not sure what those are, then we recommend you check out the various related tutorials in the about section of the help files. I N D E X:
Variables When declaring a variable using Dim, we need to supply it with a two things. First the name of our variable followed by the data type. (Ie. Dim Score as Integer) FACTS: * When declaring Float & String variables, you don't need the $, # postfix symbols. However, that's only during declaration, to use those variables, you will need the appropriate post fixes * If you want Explicit Declaration see Explicit * Also see Global,Local,Static
Arrays When we declare an array, we give Dim a NAME and a SIZE. Unlike variables, which can only store a single piece of information at a time, arrays act like containers and can hold collections of information within them. You can think of the information being stored much like a sequential list. Where each element of the set has it's own unique coordinate. These coordinates are called "Array Indexes". We use the Array Index to both store and retrieve any particular piece of information from within each array. When we declare an array, we give Dim a NAME and a SIZE. Unlike variables, which can only store a single FACTS: * Array Indexes are zero inclusive. That means 0,1,2, etc .. up to the Number of Elements are valid Indexes. * Arrays can be as large as you like. Your only limited by the amount of the memory your computer has. * When an array created (dimensioned), it will be clear. (every element inside it is set to zero) * If you DIM an array that had information in it, that information will be lost. See REDIM if you need to resize an array and preserve the contents. * Arrays declarations are parsed at compile time as you might expect. So the compiler picks up the name, number of dimensions and what type this array should be, and adds it's to it's internal table of arrays. But the actual data an arrays holds, isn't allocated/created until the Dim statement is executed at runtime. This allows the data within the array to be dynamically created / destoryed even passed to functions. Array Example For new programmers all this is quite a mouth full up front, so we'll try and go through an example to get you started. Lets imagine we wish to store five high score values within an array of the same name. [Step #1 - Creating The Array] First, we'll create our array using the appropriate Dim statement like so. This with tell PlayBASIC that we wish create an array called "HighScores" that has provision for five unique elements inside it.
[Step #2 - Creating an array then Putting Information into it] Once PlayBASIC is aware the array has been created, we can now store information within it. Lets say we wish to place the values 100 in the element #1, 200 at element #2, 300 at element #3, 400 at Element #4, and 500 at element #5.
[Step #3 - Creating an array, Putting information in it and reading it back] Once you created an Array and stored some information within it, of course at some point you'll wish to read these element back.
Multi Dimensioned Arrays One of the more advanced features of Arrays, is they can be created with up to 10 dimensions. Each Dimension is separated by a coma and can have it's own size. Generally speaking though, it's rare that you'll ever need to use arrays of more than two or in some cases three dimensions. So here's some 1D,2D & 3D examples. [1D Arrays ] A 1D array can be thought of as a list of elements. Starting from element 0 through to the size of the array.
[2D Arrays ] A 2D array can be thought of as a grid of elements. This grid has rows and columns in it. So when we create a 2D array we specify the number of rows and columns our array should be partitioned up into. The screen your looking at is a good example of a 2D array.
[3D Arrays ] A 3D array can be thought of as a volume of elements. So now the array has Width,Height and Depth. SO it's like we've taken then 2D array (has only has width and height) and added depth to it. So now we can store information in a 3D space.
User Defined Type Variables When declaring variable using Dim statement, we must give it a two things. First the name of our variable followed by the name of the user defined type. (Ie. Dim Score as MyType) * Learn About Type declarations. * See Types Tutorial!
Linked List
Typed Arrays When we declare a typed array, we give Dim a NAME and a SIZE and the datatype. FACTS: * Learn About Type declarations. * See Types Tutorial! * Array Indexes are zero inclusive. That means 0,1,2, etc .. up to the Number of Elements are valid Indexes. Which is the traditional BASIC convention. * An Arrays can be as large as you like providing it's total size (in all dimensions) is smaller than (2^28), which is massive. Only limited by the amount of the memory your computer has. * When an array is created (dimensioned), it will be clear. (every element inside it is set to zero) * If you DIM an typed array that had information in it, that information will be lost. See REDIM if you need to resize an array and preserve the contents. Example.
Pointers When declaring a pointer using Dim, we need give it a two things, first the name of our pointer followed by the data type. (Ie. Dim Score as Integer Pointer) FACTS: * Also see Pointer Examples.
Attached Example |
|||||||||||||||||||
Example Source: Download This Example
|
Related Info: | ArrayBasics | Byte | Explicit | Float | GetArrayDimensions | GetArrayElements | GetArrayStatus | Global | Integer | Local | POinter | ReDim | Static | UnDim | Word : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |