Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An Array Of Constants

Status
Not open for further replies.

mavest

Programmer
Feb 27, 2003
54
US
Is it possible to declare constants in an array type structure such that they can be accessed by index??

I realize I could load the constants into an array, but it would be preferrable to know that the array does not change value.

The constants do not actually have to be in an array, but I would like to access them by index such that I can write something like below:

'where density_const is an array of constants

dim fract(10) as double
dim mass(10) as double
dim voluem as double

for i = 0 to 10
mass(i) = density_const(i) * fract(i) * volume
next i

Thanks for your help!!!
 
You might set up the constants "array" as a function (with suitable documentation) and then you can continue to use array-like notation:
Code:
Option Explicit

Function density_const(Index As Integer)
Code:
' Density "Constants"
Code:
  Select Case Index
    Case 0: density_const = 4
    Case 1: density_const = 5
Code:
    ' etc.
Code:
  End Select
End Function

Sub test()
Dim naFract(10) As Double
Dim naMass(10) As Double
Dim nVolume As Double
  naFract(0) = 2
  naFract(1) = 3
  nVolume = 6
  CalcMass naFract, naMass, nVolume
  MsgBox naMass(0) & " " & naMass(1) & " " & naMass(2)
End Sub

Sub CalcMass(Fractarray, MassArray, Volume As Double)
Dim i As Integer
For i = LBound(Fractarray) To UBound(Fractarray)
   MassArray(i) = density_const(i) * Fractarray(i) * Volume
Next i
End Sub
 
I frankly don't understand why a regular array isn't just as good. If you define the array values once, in an initialization routine, and don't touch them elsewhere, they will be effectively constant. To change them, you'd have to change the initialization code, which is the same as changing the constant values in the code. If your users are that sophisticated and devious, then you stand no chance ;-)
Rob
[flowerface]
 
Rob, I agree with you completely. I was merely showing an alternative way to match as closely as possible the original request which stated that "The constants do not actually have to be in an array, but I would like to access them by index..."
 
Thanks for your answers!! They are all quite good!

My original idea was to just load them into an array and access them periodically throughout the program when needed.

However, the suggestion of putting them into a function is quite a novel idea!

I was hoping that an array of constants could be defined similar to initializing any other array,

something like
Public Const constArray() = {5,6,7,8}

but I've had no luck and according to answer 1 it is not possible. I did not figure it was possible, but just thought I'd ask - thanks for all!!!

-Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top