|
-
Jun 27th, 2011, 03:05 PM
#1
[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
-
Jun 27th, 2011, 03:16 PM
#2
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
-
Jun 27th, 2011, 03:18 PM
#3
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.
-
Jun 27th, 2011, 03:55 PM
#4
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?
-
Jun 27th, 2011, 04:19 PM
#5
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.
-
Jun 27th, 2011, 05:42 PM
#6
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..)
-
Jun 27th, 2011, 06:29 PM
#7
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.
-
Jun 28th, 2011, 01:44 AM
#8
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
-
Jun 28th, 2011, 02:45 AM
#9
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
-
Jun 28th, 2011, 03:41 AM
#10
Re: [RESOLVED] Extending an Enum
 Originally Posted by chris128
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
-
Jun 28th, 2011, 05:44 AM
#11
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).
-
Jun 28th, 2011, 05:58 AM
#12
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
-
Jun 28th, 2011, 08:43 AM
#13
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.
-
Jun 28th, 2011, 09:15 AM
#14
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:
<Flags()> _
Public Enum GenericRights As Integer
GENERIC_READ = &H80000000
GENERIC_WRITE = &H40000000
GENERIC_EXECUTE = &H20000000
GENERIC_ALL = &H10000000
End Enum
Public Enum MappedGenericRights As Integer
FILE_GENERIC_EXECUTE = FileSystemRights.ExecuteFile Or FileSystemRights.ReadPermissions Or FileSystemRights.ReadAttributes Or FileSystemRights.Synchronize
FILE_GENERIC_READ = FileSystemRights.ReadAttributes Or FileSystemRights.ReadData Or FileSystemRights.ReadExtendedAttributes Or FileSystemRights.ReadPermissions Or FileSystemRights.Synchronize
FILE_GENERIC_WRITE = FileSystemRights.AppendData Or FileSystemRights.WriteAttributes Or FileSystemRights.WriteData Or FileSystemRights.WriteExtendedAttributes Or FileSystemRights.ReadPermissions Or FileSystemRights.Synchronize
FILE_GENERIC_ALL = FileSystemRights.FullControl
End Enum
Public Shared Function MapGenericRightsToFileSystemRights(ByVal OriginalRights As FileSystemRights) As FileSystemRights
Dim MappedRights As FileSystemRights = Nothing
If CBool(OriginalRights And GenericRights.GENERIC_EXECUTE) Then
MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_EXECUTE, FileSystemRights)
End If
If CBool(OriginalRights And GenericRights.GENERIC_READ) Then
MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_READ, FileSystemRights)
End If
If CBool(OriginalRights And GenericRights.GENERIC_WRITE) Then
MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_WRITE, FileSystemRights)
End If
If CBool(OriginalRights And GenericRights.GENERIC_ALL) Then
MappedRights = CType(MappedRights Or MappedGenericRights.FILE_GENERIC_ALL, FileSystemRights)
End If
Return MappedRights
End Function
and then to use it:
vb.net Code:
'Get full bit mask of permissions
Dim FullPermissions As FileSystemRights = AccessRule.FileSystemRights
'Add standard/specific permissions that were defined by the generic permissions
FullPermissions = FullPermissions Or MapGenericRightsToFileSystemRights(FullPermissions)
'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
'permissions so we dont need them anymore
FullPermissions = DirectCast(FullPermissions << 8, FileSystemRights)
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
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
|