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

  
; Declare the Score as an Integer Variable
  Dim Score As Integer
  Score=100
  
; Declare the Distance# as a Float Variable
  Dim Distance As Float
  Distance#=123.45
  
; Declare the Name as a String Variable
  Dim Name As String
  Name$="Kevin"
  


Top







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.



Top







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.

  
; Use DIM to Create an array called Highscores.
  Dim HighScores(5)
  



      [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.

  
; Create the Array
  Dim HighScores(5)
  
; Now store our information within the HighScores() Array.
  HighScores(1= 100
  HighScores(2= 200
  HighScores(3= 300
  HighScores(4= 400
  HighScores(5= 500
  



      [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.


  
; Create the Array
  Dim HighScores(5)
; Now store our information within the HighScores() Array.
  HighScores(1= 100
  HighScores(2= 200
  HighScores(3= 300
  HighScores(4= 400
  HighScores(5= 500
  
;  Display the contents of our Highscores array
  
; Show the value stored in HighScores Array at ArrayIndex #1
  Print HighScores(1)
; Show the value stored in HighScores Array at ArrayIndex #2
  Print HighScores(2)
; Show the value stored in HighScores Array at ArrayIndex #3
  Print HighScores(3)
; Show the value stored in HighScores Array at ArrayIndex #4
  Print HighScores(4)
; Show the value stored in HighScores Array at ArrayIndex #5
  Print HighScores(5)
  
; Display the Screen and wait for a key to be pressed.
  Sync
  WaitKey
  




Top






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.

  
;A 1D Array called "MyList"
  Dim MyList(100)
  



     [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.

  
;A 2D Array called "MyGrid"
  Dim GridArray(10,20)
  



     [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.

  
;A 3D Array called "MyVolume"
  Dim MyVolume(10,20,30)
  





Top









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!



  
; Declare a Typed variable
  Type tPerson
     name$
     age
  EndType
  
  Dim Me As tPerson
  
  me.name$="Bill"
  Me.Age=21
  





Linked List

  
; Declare a Typed variable
  Type tPerson
     name$
     age
  EndType
  
  
  
; Declare a Typed variable with Linked list support
  Dim Person As tPerson List
  
; Add a person
  Person = New tPerson
  Person.name$="Sam"
  Person.Age=55
  
; add another person
  Person = New tPerson
  Person.name$="Michelle"
  Person.Age=32
  
; Display the people in the list
  For Each Person()
     Print Person.name$+"  "+Str$(Person.Age)
  Next
  
  Sync
  WaitKey
  
  
  



Top






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.

  
; Declare a Typed variable
  Type tPerson
     name$
     age
  EndType
  
  
; Declare a Typed variable with Link list support
  Dim People(2As tPerson
  
; Init This person
  People(1= New tPerson
  People(1).name$="Sam"
  People(1).Age=55
  
; Init This person
  People(2= New tPerson
  People(2).name$="Michelle"
  People(2).Age=32
  
; Display the people in the list
  For lp=1 To 2
     Print People(lp).name$+"  "+Str$(People(lp).Age)
  Next
  
  Sync
  WaitKey
  
  
  



Top







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.
  
  
; Declare a generic pointer
  Dim Ptr As Pointer
  
; Declare a byte pointer
  Dim bPtr As Byte Pointer
  
; Declare a word pointer
  Dim wPtr As Word Pointer
  
; Declare a integer pointer
  Dim iPtr As Integer Pointer
  
; Declare a float pointer
  Dim fPtr As Float Pointer
  
; Declare a String Reference pointer
  Dim sPtr As StringRef Pointer
  
; delcare typed pointer
  Type Vertex
     X#,y#,Z#
  EndType
  Dim VertexPtr As Vertex Pointer
  
  



Top







Attached Example


Top





 
Example Source: Download This Example
; ---------------------------------------------------------------------
;  DIM EXAMPLE..
;  ==============
;  This Program creates an ARRAY using DIM then stores and display
;  this information on the screen.
;---------------------------------------------------------------------
  
;  Dimension an Array Called "Scores" . This array can hold 6
; elements, ranging from 0 through To 5.
  Dim Scores(5)
  
  
; Store the value 1000 in our Scores array at Element #1
  Scores(1= 1000
  
; Store the value 3000 in our Scores array at Element #2
  Scores(2= 3000
  
; Store the value 5000 in our Scores array at Element #3
  Scores(3= 5000
  
; Store the value 10000 in our Scores array at Element #4
  Scores(4= 10000
  
; Store the value 50000 in our Scores array at Element #5
  Scores(5= 50000
  
  
; Create a Loop that will count the LP variable from 1 to 5
  For lp=1 To 5
   ; Print the value stored in score array, using the LP
   ; variable As the elements index.
     Print Scores(lp)
  Next lp
  
  
; Display a Message on the screen.  Showing the user, the program is
; waiting For input.
  Print "Press Any Key"
  
; Tell Play Basic To Display the Screen now.
  Sync
  
; Wait For the user to press a key
  WaitKey
  
 
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