Enumerations as array indices, like Swift

I’ve often come across the need to store items in an array (eg string or integer values) that I want to refer to again without remembering the index. I believe Swift has this capability effectively using enumerations to refer to elements in the array.

I see this isn’t in the documentation for enums but by using a simple extend method you can get close this this functionality.


Var dArray(10) As Double  //could be any size, as long as at least number of enumerations

//dimension is an enumeration with the following elements:
//.length, .width, .height, .scale

//this doesn't work
'dArray(dimension.length) = 100
'dArray(dimension.width) = 20

//but this does
dArray(dimension.length.index) = 100
dArray(dimension.width.index) = 20
dArray(dimension.height.index) = 0.5
dArray(dimension.scale.index) = 0.1

Dim Volume As Integer = dArray(dimension.length.index) * dArray(dimension.width.index) _
* darray(dimension.height.index) * dArray(dimension.scale.index)

MessageBox ( "Volume = " + Volume.ToString)

The extend function is simply:

Public Function index(extends value as window1.dimension) As integer
  return integer(int64(value))
End Function

It would be nice if the array could be dimensioned automatically to fit the enum size - but I don’t think introspection does that. It would also be nice if something like .index was available as standard on enums so as to not have to extend every enum separately.

Using constants in a class as labels for indexes

Class DimensionIndex

  Public Const length as Number = 0
  Public Const width as Number = 1
  Public Const height as Number = 2
  Public Const scale as Number = 3

End Class

Var dArray(10) As Double

dArray(DimensionIndex.length) = 100
dArray(DimensionIndex.width) = 20
dArray(DimensionIndex.height) = 0.5
dArray(DimensionIndex.scale) = 0.1

Dim Volume As Integer = dArray(DimensionIndex.length) * dArray(DimensionIndex.width) _
* darray(DimensionIndex.height) * dArray(DimensionIndex.scale)

MessageBox ( "Volume = " + Volume.ToString)

Why not using a Dictionary?

  • Don’t care about the length of the Array.
  • No need to extend every Enumeration.

Var dDict As New Dictionary

dDict.Value(dimension.length) = 100
dDict.Value(dimension.width) = 20
dDict.Value(dimension.height) = 0.5
dDict.Value(dimension.scale) = 0.1

Var volume As Integer = dDict.Value(dimension.length) * dDict.Value(dimension.width) _
* dDict.Value(dimension.height) * dDict.Value(dimension.scale)

MessageBox ( "Volume = " + volume.ToString)

Good alternatives here.
I find the enum approach is a bit more strongly typed than using the constants approach (though that would be the simplest to set up). It also forces unique values for each index which is useful if the number of elements is quite large or where you need to make changes as you develop.
Yes, I thought about dictionary as it uses enums directly but in this case I was saving the array as part of an SQL database - so an array seemed simpler.