Solved

Arrays of Constants

Posted on 1998-12-02
10
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

729 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