storing an enum-constant in a collection
Hi
I tried this, but I encountered some problems
many thanks in advance
OlimilO
I tried this, but I encountered some problems
|
|
Jabaco Source |
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 |
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 f As MyEnum = MyEnum.Flag2 MsgBox f col = New Collection Dim b As Boolean b = col.Add(f) MsgBox b ' why false? Dim f1 As MyEnum 'f1 = col.Item(1) 'ClassCastException: java.lang.Long cannot be cast to MyEnum 'why? b = col.Add(1234) MsgBox b ' why false? Dim l As Integer'Long l = col.Item(1) 'f1 = Cast(l, MyEnum) 'does not compile - why? f1 = l MsgBox l & " " & f1 l = col.Item(2) f1 = l MsgBox l & " " & f1 End Sub |
many thanks in advance
OlimilO
Collections are used to store VBVariant Objects
Hi,
Java Collections are used to store VBVariant Objects rather than simple scalar variables.
An Enum is a non-Object. To store Enums in a Collection, you'ld have to assign the Enum value to an instance of VBVariant.
VBVariant is a "wrapper object" around various data types including a normal integer.
Happy experimenting!
A1880
Java Collections are used to store VBVariant Objects rather than simple scalar variables.
An Enum is a non-Object. To store Enums in a Collection, you'ld have to assign the Enum value to an instance of VBVariant.
VBVariant is a "wrapper object" around various data types including a normal integer.
Happy experimenting!
A1880
This post has been edited 1 times, last edit by "A1880" (Mar 4th 2009, 12:40pm)
|
|
Jabaco Source |
1 |
MsgBox b ' why false?
|
Next Framework-update will fix that. FYI: I'll publish the access to the subversion repository very soon - I think that will speedup the future development of the framework.
|
|
Jabaco Source |
1 |
f1 = Cast(l, MyEnum) 'does not compile - why?
|
Beta2 can handle that. Sorry for the procrastination. I have smaller problems with the language design. Some modifications are more extensive than I thought.
Hi,
another try to store enums in collections.
I have used VBVariant as wrapper objects.
Currently, casting of enums does not really work.
Enjoy!
A1880
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
Similar threads
-
Tips, Tricks, Samples & Tutorials »-
TypeOf Is
(Feb 8th 2009, 2:44pm)
-
Suggestions »-
A better JNI implementation...JNA?
(Feb 23rd 2009, 8:21pm)
-
Allgemeine Themen, Fragen und Diskussionen »-
Dim A: A = Array("first", "second", "third")
(Jan 18th 2009, 12:46pm)
-
General topics, questions and discussions »-
Jabaco compiler does not optimize
(Jan 13th 2009, 10:04pm)
