From Firewing

Reference: ArrayConstants

 [private | public] const identifier([size]) as type = "{"value {, value}"}"

Array constants are extremely useful for accessing sets of data from within your program at runtime. Like single constant declarations, array data values cannot be changed at runtime. For example,

Const arraySize As Byte = 5
Const constArray(arraySize) As Byte = {1, 2, 10, 20, 100}

Constant arrays can be accessed in the same way as you would any other variable array. For example,

' display value to terminal program, include
' CR and LF
Sub Main()
   Const constArray(2) As Byte = {100, 200}
   Dim index As Byte
   For index = 0 To UBound(constArray)
      Console.Write(CStr(constArray(index)), 13, 10) 
End Sub

You can also directly assign a constant array to a variable array, if the number of array elements are the same. For example,

Sub Main()
   Const constArray(2) As Byte = {100, 200} ' two elements
   Dim varArray(2) As Byte                  ' two elements
   varArray = constArray                    ' assign values to variable array
End Sub

You can assign constant string arrays to variable string arrays. The only caveat is that the size of each string size, as well as the number of array elements, must be the same. This is easy to do by packing out the constant array strings with spaces. For example,

Sub Main()

   ' largest string (Au revoir) is 9 characters, so
   Const menuEnglish(2) As String = {"Hello    ", "Goodbye  "}
   Const menuFrench(2) As String = {"Bonjour  ", "Au revoir"}
   Dim menu(2) As String(10) ' 9 + null terminator

   menu = menuEnglish ' menu is now set for english
   menu = menuFrench  ' menu is now set for french
End Sub

You only need to pack constant strings when performing direct assignment, like in the example above. If you just wish to access each array element individually, then no packing is required.

Constant arrays can only have a single dimension. Firewing does not support multi-dimensional constant arrays. Constant arrays use program memory and not data RAM to store their values. This is unlikely to cause problems under normal circumstances, given the amount of code space available with current PIC® microcontrollers. However, code space is not unlimited and care should be exercised if using exceptionally large constant array declarations.

Retrieved from
Page last modified on January 07, 2013, at 10:05 AM