数组可以声明为常量吗? [英] Can an array be declared as a constant?
问题描述
是否可以:
-
将数组声明为常量
Declare an array as a constant
OR
使用一种变通方法来声明一个数组,该数组可以防止添加,删除或更改元素,因此在宏的生命周期内在功能上是恒定的吗?
Use a workaround to declare an array that is protected from adding, deleting or changing elements, and therefore functionally constant during the life of a macro?
我当然可以这样做:
Const myConstant1 As Integer = 2
Const myConstant2 As Integer = 13
Const myConstant3 As Integer = 17
Const myConstant4 ...and so on
...但是它失去了使用数组的优雅.我还可以将常量加载到数组中,并在每次使用它们时重新加载它们,但是如果在使用前无法使用这些常量值重新加载数组,则可能会将代码暴露给已更改的常量"值.
...but it loses the elegance of working with arrays. I could also load the constants into an array, and reload them each time I use them, but any failure to reload the array with those constant values before use could expose the code to a "constant" value that has changed.
欢迎任何可行的答案,但理想的答案是可以设置一次,并且在修改其他代码时不需要任何更改/维护.
Any workable answer is welcome but the ideal answer is one that can be setup once and not require any changes/maintenance when other code is modified.
推荐答案
您可以使用函数返回数组并将该函数用作数组.
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
这篇关于数组可以声明为常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!