Solved

Constant arrays

Posted on 2000-04-03
13
374 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

821 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