Dim, eeDim

Dim (memory) and eeDim (eeprom) define Variables.

Syntax:

  • Dim Name[(elements)] [, NameN[(elements)]] as [static] Datatype [= InitialValue]1)
  • eeDim Name[(elements)] [, NameN[(elements)]] as Datatype [= InitialValue]2)
  • Variables defined in a class are local and static to the whole class. For example; Class „Avr“, defined in the main program are also visible in in methods of the main program, as long as you have not variables of the same name inside the method. They are not visible in any other class. You can access variables of other classes by using ClassName.Property or Method
  • In Methods dimensioned variables are local (visible only within the method) and temporary or static (static) within the method.
  • eeprom-Variables may be dimensioned only in classes or main program (of class „Avr“), not in a subroutine. They are static.

If you define a variable named „myVar“ in a class in the main program and a variable with the same name „myVar“ in one of its methods (parameter or local variable), then the variable in the method has a higher priority, e.g. the classes variable will be hidden.

static declares variables in a method as static. They are already reserved in memory from program start, will be initialized when the controller starts and will not be initialized when the method is first called such as a temporyry variable 3)). Execution speed is higher as a temporyry variable. Remember that you should not execute this method in parallel because they will all access the same variable. This can cause hard to find errors. By default, all variables of a method are declared temporary, e.g. they exist only as long as the method is executed.

Implemented as of Version 2015.r1

InitialValue is a feature with which you can optionally assign an initial value to numerischen- and string variables in the dimensioning (in methods). Here all of the indicated variables, and arrays are filled with the initial value.

dim a,b(7) as byte = 42
dim s as string = "hello"

Strings are dynamic in memory and static in eeprom. When dimensioning a string in eeprom, you must add the string length identifier (number of characters). Read and write of eeprom-Variables is very slow and the number of write accesses is limited by the hardware. Do not permanently write eeprom-Variables!

Example when dimensioning in memory

dim a,b as byte
dim c(19) as integer    ' Word-Array of 20 elements
dim s1 as string
dim s2(4) as string     ' String-Array of 5 elements
procedure hello(a as byte)
  dim c(19) as static integer    ' static Word-Array of 20 elements
  dim s1 as string
endproc

Example when dimensioning in eeprom

eedim a,b as byte
eedim c(19) as integer      ' Word-Array of 20 elements
eedim s1[20] as string      ' String of 20 byte in memory (1 string length, 19 characters)
eedim s2[10](4) as string   ' String-Array of 5 elements @ 10 byte each (1 string length, 9 characters) 
1) , 2) as of Version 2015.r1
3) Luna initializes variables, Objects and structures that can store values with nil (same as zero