Solved

Constant arrays

Posted on 2000-04-03
13
375 Views
Last Modified: 2010-05-02
How can I create an array of constants in VB6?
0
Comment
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
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

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!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

751 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