Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Constant arrays

Posted on 2000-04-03
Medium Priority
378 Views
How can I create an array of constants in VB6?
0
Question by:Gatesy
[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
• 7
• 4
• 2

LVL 10

Expert Comment

ID: 2680215
You can't.
0

LVL 10

Expert Comment

ID: 2680276
It would be appreciated if you would refrain from shooting the messenger.
0

LVL 32

Expert Comment

ID: 2680316
If you do not mind using Variants, you can assign a constant array.  Why VB does not allow this with other data types, I don't know; wish it did.

Option Explicit

Dim a As Variant

a = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
End Sub

Private Sub Command1_Click()
Static i As Long
Debug.Print a(i)
i = i + 1
If i > UBound(a) Then i = 0
End Sub
0

LVL 10

Expert Comment

ID: 2680322
In the light of the CS attitude towards point-stealing expressed in http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt2&qid=10320626  I feel I have no other option but to lock this question.

0

LVL 10

Expert Comment

ID: 2680327
"you can assign a constant array"
Erick37, but there is nothing to stop a user from doing this:

Private Sub Command1_Click()
Static i As Long
a(i) = 10
i = i + 1
If i > UBound(a) Then i = 0
End Sub

So the array is not truly constant.
0

LVL 32

Accepted Solution

Erick37 earned 300 total points
ID: 2680363
True.
It's the difference between an array of constants, and a constant array.

If a constant array is absolutely needed, make a Class Module with the array of constants.  Make the array index values read-only, ie, do not include a Property Let to assign values.
0

Author Comment

ID: 2680422
I realize that from the literal wording of my original question the answer is "You can't".  However I was hoping for some way round the problem as proposed by Erick37.

From reading the "proposed answer" and its link,  I get the impression that the expert "points" whatever they are, are more valuable to you than actually helping someone.
0

Author Comment

ID: 2680436
I realize that from the literal wording of my original question the answer is "You can't".  However I was hoping for some way round the problem as proposed by Erick37.

From reading the "proposed answer" and its link,  I get the impression that the expert "points" whatever they are, are more valuable to you than actually helping someone.
0

LVL 10

Expert Comment

ID: 2680437
Not at all, Gatesy, sorry if I gave that impression.

If you're willing to take a less than literal solution to your problem then I think Erick37 has provided a good workaround, and I think he does deserve the points in that case.

But please do remember that it is not Erick's fault that VB has a limitation in this regard. Erick gave you a way to work around your problem.

I'm stressing this as I see from your profile that you have a tendency to give "D"s

0

Author Comment

ID: 2680475
Good idea.
0

LVL 10

Expert Comment

ID: 2680482
Hm... A "B".... Still feels like you're punishing Erick for something that's not his fault.
0

Author Comment

ID: 2680520
I'm a hard man to please.
0

LVL 10

Expert Comment

ID: 2680565
A schoolteacher perhaps?
0

## Featured Post

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we aâ€¦
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 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â€¦
###### Suggested Courses
Course of the Month7 days, 17 hours left to enroll