Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very hard enum type question...;-)

Status
Not open for further replies.

krkX

Programmer
Nov 12, 2000
54
NL
Hi there,

Got a small issue...

Iv'e got this enum:


Public enum myEnum
myenumval1
myenumval2
myenumval3
myenumval4
myenumval5
myenumval6
end enum

Kay i've assigend some vars to hold the following comination:

myVar1 = myenumval1 + myenumval6
myvar2 = myenumval3 + myenumval4

Now i use the following function to readout the values seperately:

function myFunction(byval str as myEnum) as string
if (str and myenumval1) = myenumval1 then myFunction = myFunction & "myenumval1, "
if (str and myenumval2) = myenumval2 then myFunction = myFunction & "myenumval2, "
if (str and myenumval3) = myenumval3 then myFunction = myFunction & "myenumval3, "
if (str and myenumval4) = myenumval4 then myFunction = myFunction & "myenumval4, "
if (str and myenumval5) = myenumval5 then myFunction = myFunction & "myenumval5, "
if (str and myenumval6) = myenumval6 then myFunction = myFunction & "myenumval6, "
end function

debug.print myFunction(myvar1)
debug.print myFunction(myvar2)

This sould return the string based names of the combined value's but is not...

Anyone got any clues, how to go about this a bit smarter and working..?

Many thnx in adavance...

[ponder]Krk
 
Your enum values (by default) as zero based. So to begin with, the "combined" vales are the same (integer 5):

Public enum myEnum
myenumval1=0
myenumval2=1
myenumval3=2
myenumval4=3
myenumval5=4
myenumval6=5
end enum

myVar1 = myenumval1 + myenumval6 ' 0 + 5 = 5
myvar2 = myenumval3 + myenumval4 ' 2 + 3 = 5

Your function performs a binary AND operation between the parameter (integer) and the enum values. Your debug example passes the value 5, which in your enums relates to myenumval6.

(5 and 0) = 0 'myenumval1
(5 and 1) = 0 'myenumval2
(5 and 2) = 0 'myenumval3
(5 and 3) = 0 'myenumval4
(5 and 4) = 0 'myenumval5
(5 and 5) = 1 'myenumval6 --> returns string(myenumval6)

With this information you should be able to redefine your program logic to return teh correct string. Look into using the SELECT Case statements as well.

Mark
 
krkX -

If you want to add up enumeration values and then use boolean logic ("AND" operator) to see if they're set, you'll have to use powers-of-2 in your enum:
Code:
Public enum myEnum
    myenumval1=1
    myenumval2=2
    myenumval3=4
    myenumval4=8
    myenumval5=16
    myenumval6=32
end enum

Hope this helps.
Chip H.
 
Mark,
logical AND does a bitwise AND, so will give:
(5 and 0) = 0 '101 and 000 = 000
(5 and 1) = 1 '101 and 001 = 001
(5 and 2) = 0 '101 and 010 = 000
(5 and 3) = 1 '101 and 011 = 001
(5 and 4) = 4 '101 and 100 = 100
(5 and 5) = 5 '101 and 101 = 101
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Ah...you are completely correct johnwm, I should learn to get more coffee in me before I start answering questions...

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top