Solved

Constant arrays

Posted on 2000-04-03
13
371 Views
Last Modified: 2010-05-02
How can I create an array of constants in VB6?
0
Comment
Question by:Gatesy
  • 7
  • 4
  • 2
13 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680215
You can't.
0
 
LVL 10

Expert Comment

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

Expert Comment

by:Erick37
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

Private Sub Form_Load()
    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

by:caraf_g
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.

If you feel my answer is in any way unsatisfactory please ask for further information or reject this question. I would prefer it if you would reject the answer rather than grade me with a bad grade.
0
 
LVL 10

Expert Comment

by:caraf_g
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

by:
Erick37 earned 100 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Gatesy
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

by:Gatesy
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

by:caraf_g
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

by:Gatesy
ID: 2680475
Good idea.
0
 
LVL 10

Expert Comment

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

Author Comment

by:Gatesy
ID: 2680520
I'm a hard man to please.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680565
A schoolteacher perhaps?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now