Can an array be declared as a constant? Can an array be declared as a constant? arrays arrays

Can an array be declared as a constant?


You could use a function to return the array and use the function as an array.

Function ContantArray()    ContantArray = Array(2, 13, 17)End Function

enter image description here

enter image description here


I declared a String constant of "1,2,3,4,5" and then used Split to create a new array, like so:

Public Const myArray = "1,2,3,4,5"Public Sub createArray()        Dim i As Integer        A = Split(myArray, ",")        For i = LBound(A) To UBound(A)                Debug.Print A(i)        Next iEnd Sub

When I tried to use ReDim or ReDim Preserve on A it did not let me. The downfall of this method is that you can still edit the values of the array, even if you can't change the size.


How about making it a function? Such as:

Public Function myConstant(ByVal idx As Integer) As Integer    myConstant = Array(2, 13, 17, 23)(idx - 1)End FunctionSub Test()    Debug.Print myConstant(1)    Debug.Print myConstant(2)    Debug.Print myConstant(3)    Debug.Print myConstant(4)End Sub

Nobody can change it, resize it, or edit its content... Moreover, you can define your constants on just one line!