PDA

View Full Version : Enum replacement for saving double datatype



Kurious
08-20-2009, 01:47 AM
I often use Enums to store integer values in the program
For eg.
Public Enum Fruit
Apple = 10
Banana = 15
End Enum


However now what the values that I want are no more integers, I tried
Public Enum Fruit
Apple = 0.1
Banana = 0.2
End Enum
but that does not work.

What is the best way to have some variable like this. So that I should be able to say Fruit.Apple and it should give me a value of 0.1

Thanks

Aussiebear
08-20-2009, 02:19 AM
Hi Kurious, Welcome to the VBA Express forum. Enums are normally datatype "longs". I don't believe that I've seen them as decimals before.

Aussiebear
08-20-2009, 02:21 AM
As a secondary thought perhaps this might be of some use to you?

http://www.cpearson.com/excel/Enums.aspx

Bob Phillips
08-20-2009, 04:17 AM
How about a set of global constants?

Kurious
08-20-2009, 10:08 PM
Thanks Everyone.
I can create global constants but how do I pass type fruit in a function. I want a way that I keep all these under one umbrella "Fruit" and be able to give them double values.

Any other suggestions, what else can I use rather than Enum ???

Bob Phillips
08-21-2009, 12:42 AM
In a word, with constant you cannot.

As Ted said, Enums are long, so if you want to work in decimals you will have to scale up, if you can. So use, 1, 2 etc., and divide when using them.

Either that, or use a class.

Create a class called Fruit



Option Explicit

Public Property Get Apple() As Double
Apple = 0.1
End Property

Public Property Get Banana() As Double
Banana = 0.2
End Property


and use so



Public Sub Test()
Dim cFruit As Fruit

Set cFruit = New Fruit
Call TestFruit(cFruit)
End Sub

Function TestFruit(Produce As Fruit)

MsgBox Produce.Apple

End Function

Kurious
08-21-2009, 01:58 AM
Thanks XLD.

I will try using the class