如何在结构中获取组合值的名称? [英] How to get the name of combined values in structure?

查看:107
本文介绍了如何在结构中获取组合值的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是结构值:



Here is the structure values:

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





当我的值等于261(1 + 4 + 256)时,我想得到这些组合值的名称(MP2,MP4,OGG)

我不知道哪个更好用,枚举还是结构?并且

怎么办?



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?

推荐答案

这是错误的做法。相反,使用枚举类型,每个枚举成员整数值分配给1,2,4等。您将能够对枚举成员执行二进制算术,因为它们是简单的整数。现在,如果你还为这种类型添加 [Flags] 属性,方法 ToString 将只输出字符串枚举值的几个以逗号分隔的名称,表示值的ORed结果。请参阅:

http://msdn.microsoft.com/en -us / library / system.flagsattribute.aspx [ ^ ]。



如果您需要更多细节或想要使用更高级的枚举技术,特别是那些代表位集的技术,请阅读我的两篇文章,其中详细描述了相关问题,并描述了标准技术,并提供了一些高级技术,例如遍历枚举成员集(枚举它们,这非常令人惊讶,枚举类型不直接支持),枚举值的自定义字符串表示,以及更多:

枚举类型不会枚举!解决.NET和语言限制 [ ^ ],

人类可读的枚举元数据 [ ^ ]。



-SA
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


这篇关于如何在结构中获取组合值的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆