Results 1 to 14 of 14

Thread: [RESOLVED] Extending an Enum

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [RESOLVED] Extending an Enum

    Hi Guys,

    I know from .NET 3.5 onwards (or might be 3.0) we can use Exension Methods to add our own methods to any Type in the .NET Framework and this is very handy, but I'm wondering if it is possible to "extend" an Enum as well?

    The thing is, I don't mean adding a new method to the enum, I mean adding a new value to the available items in the enum.

    E.g if we had an Enum like this:

    Code:
    Public Enum Something As Integer
        ExampleItemOne = 2
        ExampleItemTwo = 4
    End Enum
    I would like to extend this to add another item like so:

    Code:
    Public Enum Something As Integer
        ExampleItemOne = 2
        ExampleItemTwo = 4
        ExampleItemThree = 8
    End Enum
    Is this possible? I suspect not... and if that is the case, can anyone think of any other way to do something similar? Basically I have an Enum that is going to be identical to an Enum already in the framework but with a couple of extra values, so I'm looking for an alternative to just including all of the values from the existing enum manually in my own enum. Hope that makes sense

    Cheers
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Extending an Enum

    I've often wished this was a feature, I actually think you can do this is C++ but not 100% sure. As for a suggestion, this might be suitable:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button1.Click
    
            '//the base "enum"
            Me.SomeMethod(BaseEnum.ExampleItemOne)
    
            '//the extended "enum"
            Me.SomeMethod(ParentEnum.ExampleItemThree)
        End Sub
    
        Public Sub SomeMethod(ByVal value As BaseEnum.SomethingValue)
            '//uses the value
            Select Case value.Value
                Case 1
                Case 2
                Case 3
                    '//the new extended value
            End Select
        End Sub
    
    End Class
    
    Public Class BaseEnum
    
        '//fields
        Public Shared ReadOnly ExampleItemOne As New SomethingValue(1)
        Public Shared ReadOnly ExampleItemTwo As New SomethingValue(2)
    
        '//constructors
        Protected Sub New()
        End Sub
    
        '//methods
    
        Public Class SomethingValue
            Public Value As Integer
    
            '//constructors
            Friend Sub New(ByVal value As Integer)
                '//ideally the only way you could instantiate this class
                '//would be within the class
                Me.Value = value
            End Sub
    
        End Class
    
    End Class
    
    Public Class ParentEnum
        Inherits BaseEnum
    
        '//fields
        Public Shared ReadOnly ExampleItemThree As New SomethingValue(3)
    
        '//constructors
        Protected Sub New()
        End Sub
    
    End Class
    

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Extending an Enum

    Ah... I see now that you are wanting to "inherit" off a framework enum. I thought it was your enum. I guess you can ignore that code.

  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Extending an Enum

    Thanks and yeah I wanted to 'inherit' one of the built in framework enums - however, after working on other parts of my app I've realised I don't actually need this now anyway but it would be handy to know if it is possible anyway. I guess you agree with my initial thought that it can't be done?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Extending an Enum

    I guess you agree with my initial thought that it can't be done?
    If you're talking about:
    Code:
    Public Enum OrientationInherited As Integer
        Inherits Orientation
    
        Both = 0
    End Enum
    
    Then no you can't.

    The specifics of it are:

    1) All Enums must inherit System.Enum
    2) Because of #1, all Enums are ValueTypes, therefore, sealed.

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Extending an Enum

    Huh I never knew value types could not be inherited. Never really thought about it I guess that answers the original question then, thanks - thread marked as resolved and rating given (with rather random comment, dont ask..)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Extending an Enum

    Agree with forum in post #5. You could

    Code:
        Public Enum Something As Integer
            ExampleItemOne = 2
            ExampleItemTwo = 4
        End Enum
    
        Public Enum SomethingElse As Integer
            ExampleItemOne = Something.ExampleItemOne
            ExampleItemTwo = Something.ExampleItemTwo
            ExampleItemThree = 3
        End Enum
    Though I don't know if it saves anything.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [RESOLVED] Extending an Enum

    If you are inheriting, you can extend an Enum of the inherited class by shadowing it:

    Code:
    Public Class PictureBoxEX
    	Inherits PictureBox
    
    	Public Shadows Enum BorderStyle
    		None = Windows.Forms.BorderStyle.None
    		FixedSingle = Windows.Forms.BorderStyle.FixedSingle
    		Fixed3D = Windows.Forms.BorderStyle.Fixed3D
    		Baroque = 3
    		Dashed = 4
    		Gilt = 5
    	End Enum
    
    End Class
    BB

  9. #9

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Extending an Enum

    but have you got to still manually do each existing one like you have done in that example? If so it doesn't really save anything
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: [RESOLVED] Extending an Enum

    Quote Originally Posted by chris128 View Post
    but have you got to still manually do each existing one like you have done in that example? If so it doesn't really save anything
    True, but it does extend the functionality of the Enum, and that could be useful in making the program more readable.

    What I had in mind was extending the designer options of a custom control. In fact my example above was faulty. Here's a modified version to illustrate what I mean:
    Code:
    Public Class PictureBoxEX
    	Inherits PictureBox
    
    	Public Shadows Property BorderStyle As BorderStyleEx
    
    	Public Enum BorderStyleEx
    		None = Windows.Forms.BorderStyle.None
    		FixedSingle = Windows.Forms.BorderStyle.FixedSingle
    		Fixed3D = Windows.Forms.BorderStyle.Fixed3D
    		Baroque = 3
    		Dashed = 4
    		Gilt = 5
    	End Enum
    
    End Class
    Note that it's the property that's shadowed here, and the Enum is renamed BorderStyleEx. The benefit is that if the user selects the BorderStyle property in the designer Properties window, it will now show the new options -- Baroque, Dashed and Gilt. In effect the Enum has been extended -- although I agree it would be nicer if we could really extend an Enum.

    BB

  11. #11

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Extending an Enum

    Ah I see - well thanks for the info and examples, like I said I don't actually need to do this in my app any more but useful to know anyway. Its just a shame you can't extend them directly, but I guess it is very rare that you would want to do this (I only wanted to do it because the FileSystemRights enum doesn't include values for the generic file system rights, GENERIC_READ, GENERIC_WRITE etc).
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] Extending an Enum

    Hi Chris, could be wrong but but is not FileSystemRights.Read the same as GENERIC_READ likewise with FileSystemRights.Write.

    Edit: Ahh no, I was getting mixed up with FILE_GENERIC_READ/WRITE
    Last edited by Milk; Jun 28th, 2011 at 07:17 AM.
    W o t . S i g

  13. #13

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Extending an Enum

    FILE_GENERIC_READ is also not the same as FileSystemRights.Read either though, that's the problem. In fact FILE_GENERIC_READ is GENERIC_READ, but it is just the name given to the mapping of which standard permissions and object specific permissions the generic permissions equate to when applied to files/folders. Or is that what you meant?

    FileSystemRights.Read is equivalent to STANDARD_RIGHTS_READ, where as FILE_GENERIC_READ is equivalent to FILE_READ_ATTRIBUTES + FILE_READ_DATA + FILE_READ_EA + STANDARD_RIGHTS_READ + SYNCHRONIZE (http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)

    The GENERIC permissions are defined in the first 4 bits of the 32 bit value that holds permissions (http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx) but the FileSystemRights enum only includes values for the last 24 bits (the specific permissions and the standard permissions)
    Last edited by chris128; Jun 28th, 2011 at 08:58 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Extending an Enum

    Just in case it helps anyone else that finds this thread through a search for the file permissions stuff, here's my solution to get the generic permissions correctly mapped to the relevant file permissions (which means you can then just do FileSystemRights.ToString and get a meaningful description of the permissions instead of just a number when generic permissions are used):

    vb.net Code:
    1. <Flags()> _
    2.     Public Enum GenericRights As Integer
    3.         GENERIC_READ = &H80000000
    4.         GENERIC_WRITE = &H40000000
    5.         GENERIC_EXECUTE = &H20000000
    6.         GENERIC_ALL = &H10000000
    7.     End Enum
    8.  
    9.     Public Enum MappedGenericRights As Integer
    10.         FILE_GENERIC_EXECUTE = FileSystemRights.ExecuteFile Or FileSystemRights.ReadPermissions Or FileSystemRights.ReadAttributes Or FileSystemRights.Synchronize
    11.         FILE_GENERIC_READ = FileSystemRights.ReadAttributes Or FileSystemRights.ReadData Or FileSystemRights.ReadExtendedAttributes Or FileSystemRights.ReadPermissions Or FileSystemRights.Synchronize
    12.         FILE_GENERIC_WRITE = FileSystemRights.AppendData Or FileSystemRights.WriteAttributes Or FileSystemRights.WriteData Or FileSystemRights.WriteExtendedAttributes Or FileSystemRights.ReadPermissions Or FileSystemRights.Synchronize
    13.         FILE_GENERIC_ALL = FileSystemRights.FullControl
    14.     End Enum
    15.  
    16.     Public Shared Function MapGenericRightsToFileSystemRights(ByVal OriginalRights As FileSystemRights) As FileSystemRights
    17.         Dim MappedRights As FileSystemRights = Nothing
    18.         If CBool(OriginalRights And GenericRights.GENERIC_EXECUTE) Then
    19.             MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_EXECUTE, FileSystemRights)
    20.         End If
    21.         If CBool(OriginalRights And GenericRights.GENERIC_READ) Then
    22.             MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_READ, FileSystemRights)
    23.         End If
    24.         If CBool(OriginalRights And GenericRights.GENERIC_WRITE) Then
    25.             MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_WRITE, FileSystemRights)
    26.         End If
    27.         If CBool(OriginalRights And GenericRights.GENERIC_ALL) Then
    28.             MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_ALL, FileSystemRights)
    29.         End If
    30.         Return MappedRights
    31.     End Function
    and then to use it:
    vb.net Code:
    1. 'Get full bit mask of permissions
    2. Dim FullPermissions As FileSystemRights = AccessRule.FileSystemRights
    3. 'Add standard/specific permissions that were defined by the generic permissions
    4. FullPermissions = FullPermissions Or MapGenericRightsToFileSystemRights(FullPermissions)
    5. 'Clear the first 8 bits as we just converted the first 4 from generic permissions to standard/specific and we dont care about the next 4 for file permissions
    6. 'permissions so we dont need them anymore
    7. FullPermissions = DirectCast(FullPermissions << 8, FileSystemRights)
    8. FullPermissions = DirectCast(FullPermissions >> 8, FileSystemRights)

    Could probably do with some tidying up but it was what I threw together last night to make it work
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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