Enumerated types

G

Guest

I have clearly failed to understand the syntax of enumerated types. Having
declared :-

Enum ACGroup
A = 1
B
C
D
End Enum

I cannot then access the members of the group. With J as a variable byte,
the compiler baulks at ACGroup.J or ACGroup(J). I simply wish to return A…D
for values of J between 1 and 4, respectively.

There are other ways of doing this, of course, and, in this trivial example,
probably better ways, but it would be nice to have this option available.

One alternative I could try, in the mean time, is to declare a string array
and access its members using J as an index. As an old (=elderly) ‘C’
programmer, I also tried ‘For stString = “A†To “D†…, but, hardly
surprisingly, VBA was having none of it. Would anyone be kind enough to
clarify the situation for me?
 
S

Scott McDaniel

I have clearly failed to understand the syntax of enumerated types. Having
declared :-

Enum ACGroup
A = 1
B
C
D
End Enum

I cannot then access the members of the group. With J as a variable byte,
the compiler baulks at ACGroup.J or ACGroup(J). I simply wish to return A…D
for values of J between 1 and 4, respectively.

Be defintion an "enumeration" would return only numeric values. AFAIK, there is no way to have an Enum return Text
values. You could build a class or function for this, as you probably know, but there is no way to do this:

ACGroup(1)

and have that return "A"

There are other ways of doing this, of course, and, in this trivial example,
probably better ways, but it would be nice to have this option available.

One alternative I could try, in the mean time, is to declare a string array
and access its members using J as an index. As an old (=elderly) ‘C’
programmer, I also tried ‘For stString = “A” To “D” …, but, hardly
surprisingly, VBA was having none of it. Would anyone be kind enough to
clarify the situation for me?

Don't believe you can use the For-Next or For Each syntax with an array, unless you work with the Ubound or LBound of
the array (although I could be wrong about that). You could do this in a Standard Module:

[General Declaration]
dim arr() As String
Dim i as integer

Function LoadArray() As Boolean
'/redim and load the array
redim arr(3)
arr(0)="A"
arr(1)="B"
arr(2)="C"
arr(3)="D"
End Function

Function GetArrayValue(ArrayIndex as Integer) as String
If ArrayIndex <= ubound(arr) Then GetArrayValue= arr(ArrayIndex)
End If

You'd have to call LoadArray when the app started, then anywhere you needed a value from the array just call
GetArrayValue(xx)





Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G

Guest

Thanks for the prompt reply, Scott. I now understand enumerated types rather
better. It seems as if accessing an array by index is, after all, the best
way of going about the job but I have to initialize the thing and it all
looks clumsy. It would be almost as easy to forget the For...Next loop and
set the required values in four sequential explicit statements. That, too,
doesn't look too elegant but it would be much worse with 40 values to set,
instead of 4.

Come back Kernighan & Ritchie. All is forgiven. I must be getting old.
--
Peter Hallett


Scott McDaniel said:
I have clearly failed to understand the syntax of enumerated types. Having
declared :-

Enum ACGroup
A = 1
B
C
D
End Enum

I cannot then access the members of the group. With J as a variable byte,
the compiler baulks at ACGroup.J or ACGroup(J). I simply wish to return A…D
for values of J between 1 and 4, respectively.

Be defintion an "enumeration" would return only numeric values. AFAIK, there is no way to have an Enum return Text
values. You could build a class or function for this, as you probably know, but there is no way to do this:

ACGroup(1)

and have that return "A"

There are other ways of doing this, of course, and, in this trivial example,
probably better ways, but it would be nice to have this option available.

One alternative I could try, in the mean time, is to declare a string array
and access its members using J as an index. As an old (=elderly) ‘C’
programmer, I also tried ‘For stString = “A†To “D†…, but, hardly
surprisingly, VBA was having none of it. Would anyone be kind enough to
clarify the situation for me?

Don't believe you can use the For-Next or For Each syntax with an array, unless you work with the Ubound or LBound of
the array (although I could be wrong about that). You could do this in a Standard Module:

[General Declaration]
dim arr() As String
Dim i as integer

Function LoadArray() As Boolean
'/redim and load the array
redim arr(3)
arr(0)="A"
arr(1)="B"
arr(2)="C"
arr(3)="D"
End Function

Function GetArrayValue(ArrayIndex as Integer) as String
If ArrayIndex <= ubound(arr) Then GetArrayValue= arr(ArrayIndex)
End If

You'd have to call LoadArray when the app started, then anywhere you needed a value from the array just call
GetArrayValue(xx)





Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G

Guest

One of two ways. One of which you touched on.
1. Create an Array and use a numeric counter retrieve the values (I prefer
to use a Long data type for counter varialbes because they align on a word
boundry.

Dim varLetters as Variant
Dim lngCtr As Long
Dim strOneLetter as String

varLetters = Array("A","B","C","D")

strOneLetter = varLetters(lngCtr)

2. Create a string with the letters and use a counter variable and the Mid
function to retrive a value:

Dim strAllLetters as String
Dim lngCtr As Long
Dim strOneLetter as String

strAllLetters = "ABCD"

strOneLetter = Mid(strAllLetters, lngCtr,1)
 
G

Guest

Dave,
Having concluded, from the earlier reply, that the magic '4-liner' was
unlikely to emerge, I have done something similar to your first suggestion.
Your method of initialising the array is, however, much neater than mine.
That will allow me to tidy things up a bit. Your second suggestion is also
an interesting one. I might yet try that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top