Solved

Arrays of Constants

Posted on 1998-12-02
10
318 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now