|
-
Jan 16th, 2006, 09:06 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Test if argument is part of enumaration
The question is straight forward how can I test is an argument passed down a function is part of an enummeration in one statment.
Example enumeration:
Public Enum EnumBaseKeys
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
End Enum
Example Function:
Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
now how to check if Hkey is a member of EnumBasekeys?
Thanks in advance.
Last edited by Hack; Jan 18th, 2006 at 07:51 AM.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jan 16th, 2006, 12:31 PM
#2
Junior Member
Re: Test if argument is part of enumaration
VB Code:
Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
If Hkey = HKEY_CLASSES_ROOT _
Or Hkey = HKEY_CURRENT_USER _
Or Hkey = HKEY_LOCAL_MACHINE _
Or Hkey = HKEY_USERS Then
'it is a member
Else
'it is not a member
End If
End Function
-
Jan 16th, 2006, 12:53 PM
#3
Re: Test if argument is part of enumaration
I don't think there is any easy way to do this. From a functional standpoint, all an enum does is create a list of constants with a namespace. Your enum would be identical to this as far as the compiler was concerned:
VB Code:
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
-
Jan 16th, 2006, 12:59 PM
#4
Thread Starter
Fanatic Member
Re: Test if argument is part of enumaration
 Originally Posted by EricDV
VB Code:
Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
If Hkey = HKEY_CLASSES_ROOT _
Or Hkey = HKEY_CURRENT_USER _
Or Hkey = HKEY_LOCAL_MACHINE _
Or Hkey = HKEY_USERS Then
'it is a member
Else
'it is not a member
End If
End Function
Thanks to both of you, I hoped there was an way to compare to them all at once. but that seems to be wishfull thinking.
Thanks for taking the time to reply.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jan 16th, 2006, 02:17 PM
#5
Re: Test if argument is part of enumaration
 Originally Posted by Dnereb
Thanks to both of you, I hoped there was an way to compare to them all at once. but that seems to be wishfull thinking.
Sure there is! 
VB Code:
Public Enum EnumBaseKeys
[_ENUMBASEKEYS_FIRST] = &H80000000
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
[_ENUMBASEKEYS_LAST] = &H80000003
End Enum
' .......
Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
If ((Hkey >= [_ENUMBASEKEYS_FIRST]) And (Hkey <= [_ENUMBASEKEYS_LAST]) Then
' ...
End If
End Function
The brackets around the FIRST and LAST members hide them from the Intellisense menu, so you won't accidentally use them when assigning values
-
Jan 16th, 2006, 05:46 PM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] Test if argument is part of enumaration
Thanks Penagate!
Your Briljant!
Saves me a lot of trouble, not on this enumaration only but on several others I plan to use.
As you've might have guessed is this just for a registerclass. But I find it very usefull to use enums for arguments in classes.
If you don't mind I'll copy your answer (with a reference) to another forum
that gave the same "No can do" as a result.
Thanks.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jan 16th, 2006, 05:49 PM
#7
Re: [RESOLVED] Test if argument is part of enumaration
No problems, spread the knowledge!
-
Jan 16th, 2006, 05:55 PM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Test if argument is part of enumaration
If you like to look at it....
Copy of penagate's reply
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jan 16th, 2006, 06:46 PM
#9
Re: [RESOLVED] Test if argument is part of enumaration
I must have missed something here because I honestly don't see the advantage of adding two hidden enumeration values. Since the values follow each other why not simply use HKEY_CLASSES_ROOT and HKEY_USERS instead of the two hidden values? It might be a nice trick to be able to do so, but I really can't see the advantage... please enlighten me.
-
Jan 16th, 2006, 06:50 PM
#10
Re: [RESOLVED] Test if argument is part of enumaration
Well, the advantage is that it makes it clear which is the first and last, if you need to add any more enumeration members you don't have to hunt through the code to change the constants used, simply update the values of the _FIRST and _LAST members.
-
Jan 17th, 2006, 04:53 PM
#11
Thread Starter
Fanatic Member
Re: [RESOLVED] Test if argument is part of enumaration
It is as penagate said but the enumeration doesn't have to be continues either:
This special sequence works fine as well.
Code:
Public Enum EnumBaseKeys
[_DOCTYPES_FIRST] = 0
ALL = 0
ACCESS = 1
EXCEL = 2
WORD = 4
POWERPOINT = 8
ACAD_DWG = 16
ACAD_DXF = 32
EXCLUDE = 64
[_DOCTYPES_LAST] = 127 '(64 * 2) - 1
End Enum
Although the example is a bit silly you coud still test for last and first because all the numbers in the gaps are a combination of two or more enummerations keys (To filter them you could use bitwise comparison).
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Jan 17th, 2006, 05:12 PM
#12
Re: [RESOLVED] Test if argument is part of enumaration
Well that would of course depend on if a setting can be a combination of different values or not. I can buy the argument that it would be easier to add new enumeration values even though I still think it is a bit of an overkill to do so with an enumeration of the HKEY's available in the registry. Future versions of Windows might have new root keys but I doubt it.
-
Jan 18th, 2006, 11:45 AM
#13
Thread Starter
Fanatic Member
Re: [RESOLVED] Test if argument is part of enumaration
I don't see why it's overkill a enummeration doesn't gulp memory as far as I know
it's just a bit more typing now
(Class development) to avoid typing and mistyping later.
About the register...well take enough time and I'm almost sure it will be extended.
in the future, but I doubt my code will survive that long.
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|