Results 1 to 13 of 13

Thread: [RESOLVED] Test if argument is part of enumaration

  1. #1

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Resolved [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.

  2. #2
    Junior Member EricDV's Avatar
    Join Date
    Dec 2005
    Location
    MN
    Posts
    16

    Re: Test if argument is part of enumaration

    VB Code:
    1. Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
    2.     If Hkey = HKEY_CLASSES_ROOT _
    3.         Or Hkey = HKEY_CURRENT_USER _
    4.         Or Hkey = HKEY_LOCAL_MACHINE _
    5.         Or Hkey = HKEY_USERS Then
    6.         'it is a member
    7.  
    8.     Else
    9.         'it is not a member
    10.     End If
    11. End Function
    EricDV-Programmer

  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. Const HKEY_CLASSES_ROOT = &H80000000
    2. Const HKEY_CURRENT_USER = &H80000001
    3. Const HKEY_LOCAL_MACHINE = &H80000002
    4. Const HKEY_USERS = &H80000003

  4. #4

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Test if argument is part of enumaration

    Quote Originally Posted by EricDV
    VB Code:
    1. Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
    2.     If Hkey = HKEY_CLASSES_ROOT _
    3.         Or Hkey = HKEY_CURRENT_USER _
    4.         Or Hkey = HKEY_LOCAL_MACHINE _
    5.         Or Hkey = HKEY_USERS Then
    6.         'it is a member
    7.  
    8.     Else
    9.         'it is not a member
    10.     End If
    11. 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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. Public Enum EnumBaseKeys
    2.     [_ENUMBASEKEYS_FIRST] = &H80000000
    3.     HKEY_CLASSES_ROOT = &H80000000
    4.     HKEY_CURRENT_USER = &H80000001
    5.     HKEY_LOCAL_MACHINE = &H80000002
    6.     HKEY_USERS = &H80000003
    7.     [_ENUMBASEKEYS_LAST] = &H80000003
    8. End Enum
    9.  
    10. ' .......
    11.  
    12. Public Function Makekey(Optional Hkey As EnumBaseKeys, Optional Subkey As String) As Boolean
    13.     If ((Hkey >= [_ENUMBASEKEYS_FIRST]) And (Hkey <= [_ENUMBASEKEYS_LAST]) Then
    14.         ' ...
    15.     End If
    16. 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

  6. #6

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Test if argument is part of enumaration

    No problems, spread the knowledge!

  8. #8

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  11. #11

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  13. #13

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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
  •  



Click Here to Expand Forum to Full Width