PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Code condensing: Unknown enum type-VBForums
Results 1 to 4 of 4

Thread: Code condensing: Unknown enum type

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    535

    Code condensing: Unknown enum type

    Hi, I have around 10 different functions that all work. But i was thinking surely there is a way to only use One function. An example.

    Code:
            Private Function Groups(message As String, capture As ImGroups) As String
                Return Me._mIncomingMessage.Match(message).Groups(capture).Value
            End Function
    Message is simply a string message that is matched agasint.
    ImGroups is an enum
    _mIncomingMessage is a regular expression.
    ToTitleCase() is just an extension.

    I have Ten or so of these functions that all use the same regex but different enum. So my question is can i parse a enum type instead of 10 functions?

    I was thinking like

    Code:
            Private Function Groups(of T)(message As String, capture As T, phase As string) As String
                dim [enum] = DirectCast(System.Enum.Parse(GetType(T), phase.ToTitleCase()), T)
    
                Return Me._mIncomingMessage.Match(message).Groups([enum].ToString()).Value
            End Function
    But this don't seem right.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,755

    Re: Code condensing: Unknown enum type

    Keep in mind that every Enum is an integer under the hood, and you can say which integer each value in the Enum has, so you can do either of these:
    Code:
    Public Enum MySet
     Item1
     Item2
    End Enum
    
    Public Enum MySet
     Item1 = 1
     Item2 = 2
    End Enum
    What this means is that, if you have not already set values for these enums, you could set values for them and cast everything to an integer. Presumably, for your functions, "All enum values that equate to 1 should perform like X, while those that equate to 2 should perform like Y". You could then pass in the enum converted to an integer.

    However, I also don't have as much objection to the code you have as you do. All it really looks like is a series of overloads that take a different type for the second argument, which doesn't bother me any. What I'm suggesting is that the second argument could always be an Integer, which would result in a single function, but you'd lose the real advantage of an enum, which is clarity. Right now, you are saying, "this method requires an X", whereas consolidating to an integer would change that to, "this method requires an integer....and you'd best know what that integer means."
    My usual boring signature: Nothing

  3. #3
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,269

    Re: Code condensing: Unknown enum type

    Quote Originally Posted by Shaggy Hiker View Post
    Keep in mind that every Enum is an integer under the hood
    ... Not quite true ... for eg:


    VB.Net Code:
    1. Public Enum MySet As Long
    2.  Item1 = 1
    3.  Item2 = 2
    4. End Enum

    ... I guess technically this could be called an Int64

    Kris

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,755

    Re: Code condensing: Unknown enum type

    Yeah, but still an integer, if not an Integer. Though I've never seen an enum used as a Long before. The only reason I would think to do something like that is if you have a LOT of values and the enum is really a set of flags.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width