[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
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.
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
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
Re: [RESOLVED] Extending an Enum
Quote:
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
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).
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
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)
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