473,666 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2208
Hi Raymond,

See it this helps:

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

Public Enum MyEnumWithoutFl ags
A = 1
B = 2
End Enum

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Call f(MyEnumWithFla gs.A)
Call f(MyEnumWithout Flags.A)
End Sub

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

objType = e.GetType
If objType.GetCust omAttributes(Ge tType(FlagsAttr ibute),
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.c om> escribió en el
mensaje news:Oe******** ******@TK2MSFTN GP12.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*****@NOSPAM sogecable.com> wrote in message
news:el******** ******@TK2MSFTN GP09.phx.gbl...
Hi Raymond,

See it this helps:

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

Public Enum MyEnumWithoutFl ags
A = 1
B = 2
End Enum

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Call f(MyEnumWithFla gs.A)
Call f(MyEnumWithout Flags.A)
End Sub

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

objType = e.GetType
If objType.GetCust omAttributes(Ge tType(FlagsAttr ibute),
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.c om> escribió en el
mensaje news:Oe******** ******@TK2MSFTN GP12.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
4023
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 I have to create static functions to convert these enum values to/from the actual comparison operator strings. As I understand it, I can't add methods to an enum, so is there any more elegant way to do this?
1
1603
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
4586
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
7118
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 how does one override the function to have some values return a different string? For example, suppose I have an enum with value for the types of DVD media like public enum DVD_Media {
4
6674
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 = Blah.a | Blah.b; So what is the purpose of the Flags attribute? I understand it's potentially useful for reflection, so that (for example) the IDE's IntelliSense knows which enums are meant to be combined, but does it
3
13897
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
5472
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 YourNationality=3 ' Set this to retrieve your real nationality End Enum
6
4614
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 the "why" of the behaviour. After dimensioning an enum of type integer, any attribute referenced seems to, by default, return the name of that attribute as a string, instead of the integer value assigned to it. The code snippet that follows...
34
11177
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.