Solved

Constant arrays

Posted on 2000-04-03
13
372 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

914 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

17 Experts available now in Live!

Get 1:1 Help Now