[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.
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
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
Re: Test if argument is part of enumaration
Quote:
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.
Re: Test if argument is part of enumaration
Quote:
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 :)
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.
Re: [RESOLVED] Test if argument is part of enumaration
No problems, spread the knowledge! :D
Re: [RESOLVED] Test if argument is part of enumaration
If you like to look at it....
Copy of penagate's reply
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.
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.
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).
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.
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.