Hi,
another try to store enums in collections.
I have used VBVariant as wrapper objects.
Currently, casting of enums does not really work.
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
Option Explicit
Public Enum MyEnum
FlagNone = 0
Flag0 = 1
Flag1 = 2
Flag2 = 4
Flag3 = 8
End Enum
Dim col As Collection
Public Sub Command1_Click()
Dim b As Boolean
Dim f As MyEnum = MyEnum.Flag2
Dim f1 As MyEnum
Dim l As Integer
Dim o As Object
Dim v As VBVariant
MsgBox "1:: MyEnum.Flag2: " & f
col = New Collection
v = e2v(f)
b = col.Add(v)
MsgBox "2:: col.Add: " & b ' why false?
v = col.Item(1)
f1 = v2e(v)
v = New VBVariant(1234)
b = col.Add(v)
MsgBox "3:: " & b ' why false?
v = col.Item(2)
l = v.intValue
'f1 = Cast(l, MyEnum) 'does not compile - why?
f1 = l
MsgBox "4:: " & l & " " & f1
o = col.Item(1)
v = Cast(o, VBVariant)
l = v.intValue
f1 = l
MsgBox "5:: " & l & " " & f1
End Sub
Private Function e2v(e As MyEnum) As VBVariant
Dim i As Integer
Dim v As VBVariant
' the following does not work (yet)
' v2i = New VBVariant(Cast(e, integer))
Select Case e
Case FlagNone:
i = 0
Case Flag0:
i = 1
Case Flag1:
i = 2
Case Flag2:
i = 4
Case Flag3:
i = 8
End Select
v = New VBVariant(i)
e2v = v
End Function
Private Function v2e(v As VBVariant) As MyEnum
' the following does not work (yet)
' v2e = Cast(v.intValue, MyEnum)
Select Case v.intValue
Case 0:
v2e = myenum.flagnone
Case 1:
v2e = myenum.flag0
Case 2:
v2e = myenum.flag1
Case 4:
v2e = myenum.flag2
Case 8:
v2e = myenum.flag3
End Select
End Function
|
Enjoy!
A1880