Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the structure values:

VB
Public Structure IntValues
 Public Shared MP2 As Integer = 1
 Public Shared MP3 As Integer = 2
 Public Shared MP4 As Integer = 4
 Public Shared MOV As Integer = 16
 Public Shared OGG As Integer = 256
 Public Shared WAV As Integer = 512
End Structure


And when I have a value equal to 261 ( 1+4+256 ) I want to get the name of these combined values ( which are MP2,MP4,OGG )
And I do not know which is better to use, Enum or Structure? And
how to do that?
Posted
Updated 25-Jul-13 9:13am
v5
Comments
CHill60 25-Jul-13 14:14pm    
How are you storing this "structure"?
Leecherman 25-Jul-13 14:16pm    
hmmm, I don't know which is better to use, Enum or Structure

here is it:

Public Structure IntValues
Public Shared MP2 As Integer = 1
Public Shared MP3 As Integer = 2
Public Shared MP4 As Integer = 4
Public Shared MOV As Integer = 16
Public Shared OGG As Integer = 256
Public Shared WAV As Integer = 512
End Structure

1 solution

This is wrong approach. Instead, use the enumeration type, each enumeration member integer value assigned to 1, 2, 4, etc. You will be able to perform binary arithmetic on enumeration members as they were simple integers. Now, if you also add a [Flags] attribute to such type, the method ToString will simply output the string with several comma-separated names of enumeration value, representing the ORed result of the values. Please see:
http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx[^].

And if you need more detail or want more advanced techniques of working with enumerations, especially those representing bit sets, please read two of my articles where I describe the related problem in detail, and describe the standard techniques and also provide some advanced techniques, such as traversing the set of enumeration members (enumerating them, which is quite amazingly, is not directly supported by enumeration types), customized string representation of enumeration values, and more:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

—SA
 
Share this answer
 
Comments
Ron Beyer 25-Jul-13 18:10pm    
+5, wrong "structure" to begin with, this is exactly what enums are meant for. You can also cast them to an integer to get a sum of the set flags.
Sergey Alexandrovich Kryukov 25-Jul-13 18:34pm    
Thank you, Ron.
Type case? Not really. The cast is not needed. Please see my articles, I show how to work with bit sets, based in traditional techniques and the one I put forward.
—SA
Espen Harlinn 25-Jul-13 19:16pm    
5'ed!
Sergey Alexandrovich Kryukov 25-Jul-13 19:28pm    
Thank you, Espen.
—SA
Leecherman 25-Jul-13 20:05pm    
Thanks a lot Sergey Alexandrovich Kryukov, that's exactly what I want (5Stars)
@Ron Beyer, Yeah that's right, and by using it (Sergey Alexandrovich Kryukov's solution) the problem is solved, thanks all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900