Solved

Arrays of Constants

Posted on 1998-12-02
10
322 Views
Last Modified: 2010-05-03
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
Comment
Question by:teckee
  • 4
  • 4
  • 2
10 Comments
 

Author Comment

by:teckee
ID: 1447835
Edited text of question
0
 
LVL 3

Expert Comment

by:traygreen
ID: 1447836
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
 

Author Comment

by:teckee
ID: 1447837
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:traygreen
ID: 1447838
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1447839
Dim TheArray(0 To 4) As Integer
TheArray = Array(0,1,2,3,4)
0
 

Author Comment

by:teckee
ID: 1447840
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1447841
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
 
LVL 3

Accepted Solution

by:
traygreen earned 50 total points
ID: 1447842
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
 

Author Comment

by:teckee
ID: 1447843
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
 
LVL 3

Expert Comment

by:traygreen
ID: 1447844
Glad to help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question