473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Check an enum for an attribute

I have a function that takes a value is as System.Enum and I want to be able
to look at that value and determine if it is a regular enum or an enum that
has a <Flag()> attribute set on it.

I am trying to write a wrapper function that takes an enum and an integer
value and validates if the value is actually part of the enum. Just trying
to use .IsDefined will not return accurate results (as far as I can find) if
the enum is a flag so if it is I want to jump to some other code and try to
validate another way.

I have been looking all over and can't find a way (probably using
reflection) to inspect this and look for any applied attributes.
--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-5973
Blog: http://spaces.msn.com/members/rcassick/
Dec 1 '05 #1
2 2179
Hi Raymond,

See it this helps:

<Flags()> _
Public Enum MyEnumWithFlags
A = 1
B = 2
End Enum

Public Enum MyEnumWithoutFlags
A = 1
B = 2
End Enum

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call f(MyEnumWithFlags.A)
Call f(MyEnumWithoutFlags.A)
End Sub

Sub f(ByVal e As System.Enum)
Dim objType As Type

objType = e.GetType
If objType.GetCustomAttributes(GetType(FlagsAttribute ),
True).GetLength(0) > 0 Then
MsgBox("It has Flags attribute")
Else
MsgBox("It does not have Flags attribute")
End If

End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Ray Cassick (Home)" <rc************@enterprocity.com> escribió en el
mensaje news:Oe**************@TK2MSFTNGP12.phx.gbl...
I have a function that takes a value is as System.Enum and I want to be
able to look at that value and determine if it is a regular enum or an enum
that has a <Flag()> attribute set on it.

I am trying to write a wrapper function that takes an enum and an integer
value and validates if the value is actually part of the enum. Just trying
to use .IsDefined will not return accurate results (as far as I can find)
if the enum is a flag so if it is I want to jump to some other code and
try to validate another way.

I have been looking all over and can't find a way (probably using
reflection) to inspect this and look for any applied attributes.
--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-5973
Blog: http://spaces.msn.com/members/rcassick/

Dec 1 '05 #2
That is VERY close to what I ended up coming up with :) Spent some more time
working on it last night after I posted and ended up getting it.

Thanks for the post.

I am always in awe of the way people help others here.

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
Hi Raymond,

See it this helps:

<Flags()> _
Public Enum MyEnumWithFlags
A = 1
B = 2
End Enum

Public Enum MyEnumWithoutFlags
A = 1
B = 2
End Enum

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call f(MyEnumWithFlags.A)
Call f(MyEnumWithoutFlags.A)
End Sub

Sub f(ByVal e As System.Enum)
Dim objType As Type

objType = e.GetType
If objType.GetCustomAttributes(GetType(FlagsAttribute ),
True).GetLength(0) > 0 Then
MsgBox("It has Flags attribute")
Else
MsgBox("It does not have Flags attribute")
End If

End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Ray Cassick (Home)" <rc************@enterprocity.com> escribió en el
mensaje news:Oe**************@TK2MSFTNGP12.phx.gbl...
I have a function that takes a value is as System.Enum and I want to be
able to look at that value and determine if it is a regular enum or an
enum that has a <Flag()> attribute set on it.

I am trying to write a wrapper function that takes an enum and an integer
value and validates if the value is actually part of the enum. Just
trying to use .IsDefined will not return accurate results (as far as I
can find) if the enum is a flag so if it is I want to jump to some other
code and try to validate another way.

I have been looking all over and can't find a way (probably using
reflection) to inspect this and look for any applied attributes.
--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-5973
Blog: http://spaces.msn.com/members/rcassick/


Dec 2 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Smith | last post by:
I have a custom enum to list comparison operators: =, <, <=, >, >= . I have created it as follows: public enum Comparator {Equal, LessThan, LessThanOrEqual,GreaterThan,GreaterThanOrEqual} Now...
1
by: | last post by:
Would be nice to specify an incremenet or decremenet as an attribute in an enum so I can for example have the following... enum SomeEnum { A = 0, B, // 2 C, // 4 D /6
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
10
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but...
4
by: Paul E Collins | last post by:
The help file says that "bit fields can be combined using a bitwise OR operation, whereas enumerated constants cannot", and yet this code works: enum Blah { a, b, c }; // without .... Blah x...
3
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
4
by: NewAlias | last post by:
How to display comments for each enum entry? 'Enum example with commnents commented Private Enum MyEnum YourName =1 ' Set this to get your real name YourAge=2 ' Set this to get your real age...
6
by: Jason Larion | last post by:
When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.