• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Arrays of Constants

Experts,

How would I declare a public array of integer constants using VB 5, ie

Public Const IntArray[9]
IntArray[0] = -1000, IntArray[1] = -2000, ....

for use in a loop such as

Dim MyVar[9] as Integer

For i = 0 to 9
  MyVar[i] = IntArray[i]
Next i

Thank You
0
teckee
Asked:
teckee
  • 4
  • 4
  • 2
1 Solution
 
teckeeAuthor Commented:
Edited text of question
0
 
traygreenCommented:
You need a pblic array (I'm not sure what you want with the Const)

Const IntStart = -1000
Const IntStep  = -1000
Const IntMax   = 9

Public IntArray(IntMax)

then initialize it...
Private Sub InitIntArray
   Dim lcv as Integer

   IntArray(0) = IntStart

   For lcv = 1 to IntMax
      IntArray(lcv) = IntStart + (lcv * IntStep)
   Next lcv

End Sub

Then do whatever you want with it.
0
 
teckeeAuthor Commented:
I would like to have constant integers and reference these constants from an array with an index .  For instance, the first constant would have the name of the constant end with 1, ie Constant1, the second constant would end with 2 ie Constant2, etc....In the program, a for loop would access the constant based on the last character of the name, ie when the For loop index is 1, Constant 1 is accessed, 2, Constant 2 is accessed, ....

There are 3 reasons I can't use an array
(1) the values are fixed, and assignment to a variable increases the chance that the value may change inadvertantly.  This cannot happen anywhere!
(2)Even if I assign to an array, I would still need some sort of case statement or many lines of code to accomplish assignment of constants to the individual/corresponding array index (unless I can determine the last character of the constant, similar to using string manipulation to find the last character of a string)
(3)I am using many constants, and would like to make my program more modular, be able to use 2-3 lines of code (ie a for loop) to make all of the assignments.  

I'd really like be able to reference constants as an array without having to declare an array variable, initialize the variable (many lines of code) with my constants, then use the array variable.


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
traygreenCommented:
Ok - you can try the following....

Public Enum eMyInts
   efirst = -1000
   esecond = -2000
End Enum

Public MyInts As eMyInts

Public Function GetVal(pSubscript As Integer) As Integer
   Select Case pSubscript
      Case 1
         GetVal = eMyInts.efirst
      Case 2
         GetVal = eMyInts.esecond
      Case Else
         GetVal = 0
   End Select
End Function

Private Sub Command1_Click()
   For lcv = 1 To 3
      MsgBox GetVal(lcv)
   Next lcv
   
End Sub

Or create a class to refernce the values and do not allow write permissions outside the class.
0
 
scrapdogCommented:
Dim TheArray(0 To 4) As Integer
TheArray = Array(0,1,2,3,4)
0
 
teckeeAuthor Commented:
TrayGreen,

Thank you for suggesting these possibilities, however, these answers all involve assigning
variables to the constants, some with more and less lines of code, then using the variables.  
Although I may have to take the approach of defining constants, then assigning them to an array using a case statement  or non looping statement, I was looking for a way to use the constants via an index and looping statement without having to assign them one by one to an array.  
Assigning to a variable could possibly lead to the values changing somewhere, and is more coding.  VB should have a way to define constants that they can be accessed using an index, similar to an array, but constants non variables?
0
 
scrapdogCommented:
You can't assign values to constants in run-time.  That defeats the purpose of a constant.  If you want to assign them using a loop you will have to use a variable.
0
 
traygreenCommented:
scrapdog is right.  I wasn't assigning a value to the set.
In the second option I was accessing the values of a "constant" enumerated set.
If you put this code in a module by itself you can access it through the function in the loop that you described......
>>>>for use in a loop such as

        Dim MyVar[9] as Integer

        For i = 0 to 9
          MyVar[i] = IntArray[i]
        Next i

would become.....

dim MyVar[9] as Integer
dim lcv as integer

for lcv = 1 to 9
  MyVar(lcv) =  GetVal(lcv)
next lcv

I am sorry if you do not like the answers I have given, but given what you have stated and the fact that there is no such beast as an array of canstants (barring an enumerated set) I think it's all you are going to get.

As stated in the second answer, I would encapsulate the set and it's access in an object with read only properties.  


0
 
teckeeAuthor Commented:
TrayGreen

Thank you for providing this answer.  An array to access constants was what I was looking for, however if that is not possible, then I'll have to use a variable, and this will save me some coding.
0
 
traygreenCommented:
Glad to help
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now