Solved

Constant arrays

Posted on 2000-04-03
13
373 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 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