ArrayConstants

 [private | public] const identifier([size]) as type = "{"value {, value}"}"
  • Private – An optional keyword which ensures that a constant array is only available from within the module it is declared. Constant arrays are private by default.
  • Public – An optional keyword which ensures that a constant array is available to other programs or modules.
  • Identifier – A mandatory constant name, which follows the standard identifier naming conventions
  • Size – An optional constant expression which defines the number of elements in the constant array. If size is omitted, the compiler will automatically compute the size based on the number of elements in the list.
  • Type – A mandatory data type. Supported types include boolean, bit, byte, sbyte, short, ushort, integer, uinteger, single, string and char.
  • Value – One or more data values.

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) 
   Next
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
   ' pack each string to match…
   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.