枚举值 [英] Enumeration values

查看:61
本文介绍了枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更像是一个C ++问题,但我想它同样适用于C#。


有没有办法声明一个枚举,其中分配给字段的值是

增加了一些而不是1.


我想要做的是创建一个枚举,其中变量增加

一些公式而不仅仅是1.我怀疑这是可能的,但它会是

很好


基本上我要做的就是


enum E

{

A = StartValue,

B = 1 * 4 + A,

C = 2 * 4 + A,

D = 3 * 4 + A

}


这是我正在努力oop一些以特定的方式打包的标志

方式(不在我的控制之下,但完成了我的微软...参见
http://msdn2.microsoft.com/en-us/library/ms902086.aspx


如果我能以某种方式自动让编译器做到这一点,那就更好了。

数学如此


enum E

{

A = StartValue,

B,

C,

D

}


它给出了正确的价值。


我肯定没有办法做到这一点但是我想我会问(我想我可以

写一个预处理器,但这需要很多工作。


谢谢,

Jon

This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.

What I would like to do is create an enum where the varaibles increment by
some formula rather than just 1. I doubt this is possible but it would be
nice

Essentially what I''m having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

This is be I''m trying to "oop" some flags that are packed in a specific
way(not under my control but done my microsoft... see
http://msdn2.microsoft.com/en-us/library/ms902086.aspx)

It would just be nicer if I could somehow automatically have the compiler do
the math so

enum E
{
A = StartValue,
B,
C,
D
}

and it give the right values.

I''m sure theres no way to do this but I thought I would ask(I guess I could
write a pre-processor but thats to much work.

Thanks,
Jon

推荐答案

Jon Slaughter< Jo *********** @ Hotmail.comwrote:
Jon Slaughter <Jo***********@Hotmail.comwrote:

这更像是一个C ++问题,但我想它同样适用于C#。


是有一种方法来声明一个枚举,其中分配给字段的值是

增加了一些而不是1.
This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.



不,我不喜欢不相信。说实话,它会被充分利用

很少,我认为这不会证明

语言的额外复杂性。

一个例外是Flags枚举。我希望当我宣布

枚举


[标志]

公共枚举值

{

Foo,

酒吧,

Baz

}


它会自动使用1,2和4。不幸的是你必须手动完成

- 这是一种使用非常广泛的模式。


-

Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet 博客: http:// www .msmvps.com / jon.skeet

如果回复小组,请不要给我发邮件

No, I don''t believe so. To be honest, it would be used sufficiently
rarely that I don''t think it would justify the extra complexity in the
language.

The one exception to that is Flags enums. I wish when I declared an
enum of

[Flags]
public enum Values
{
Foo,
Bar,
Baz
}

it would use 1, 2 and 4 automatically. Unfortunately you''ve got to do
it manually - and this is a very widely used pattern.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


你好工作

基本上我要做的就是


enum E

{

A = StartValue,

B = 1 * 4 + A,

C = 2 * 4 + A,

D = 3 * 4 +一个

}


你想要的更多


" Jon Slaughter"写道:
Hi it will work
Essentially what I''m having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

what u want more

"Jon Slaughter" wrote:

这更像是一个C ++问题,但我想它同样适用于C#。


有没有声明枚举的方式,分配给字段的值是

增加的东西而不是1.


我想要做的是创建一个枚举一些公式而不仅仅是1.增加了一些公式,而不仅仅是1.我怀疑这是可能的但是它会是

很好


基本上我要做的就是


enum E

{

A = StartValue,

B = 1 * 4 + A,

C = 2 * 4 + A,

D = 3 * 4 + A

这是我想要的oop一些以特定的方式打包的标志

方式(不在我的控制之下,但完成了我的微软...参见
http://msdn2.microsoft.com/en-us/library/ms902086.aspx


如果我能以某种方式自动让编译器做到这一点,那就更好了。

数学如此


enum E

{

A = StartValue,

B,

C,

D

}


它给出了正确的价值。


我肯定没有办法做到这一点但是我想我会问(我想我可以

写一个预处理器,但这需要很多工作。


谢谢,

Jon
This is more of a C++ question but I guess it applies equally well to C#.

Is there a way to declare an enum where the values assigned to the fields is
incremented by something rather than 1.

What I would like to do is create an enum where the varaibles increment by
some formula rather than just 1. I doubt this is possible but it would be
nice

Essentially what I''m having to do is

enum E
{
A = StartValue,
B = 1 * 4 + A,
C = 2 * 4 + A,
D = 3 * 4 + A
}

This is be I''m trying to "oop" some flags that are packed in a specific
way(not under my control but done my microsoft... see
http://msdn2.microsoft.com/en-us/library/ms902086.aspx)

It would just be nicer if I could somehow automatically have the compiler do
the math so

enum E
{
A = StartValue,
B,
C,
D
}

and it give the right values.

I''m sure theres no way to do this but I thought I would ask(I guess I could
write a pre-processor but thats to much work.

Thanks,
Jon


9月23日晚上11点35分,Jon Skeet [C#MVP]< sk ... @ pobox.comwrote:
On Sep 23, 11:35 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

>

不,我不相信。说实话,它会被使用很少

很少我不认为这会证明

语言的额外复杂性。
>
No, I don''t believe so. To be honest, it would be used sufficiently
rarely that I don''t think it would justify the extra complexity in the
language.



Jon你真的很讨厌Enums我注意到了。就像我讨厌嵌套类一样。

特别是在最后一个帖子之后,我学会了''嵌套类

就像拔牙一样。大声笑。努力学习。


RL

Jon you really hate Enums I notice. Just like I hate nested classes.
Especially after that last thread, where I ''learned'' nested classes
like pulling teeth. LOL. Learning the hard way.

RL


这篇关于枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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