本文介绍了如何将枚举分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从声明的枚举中分配新变量

How to assign new variable from declared enum

public enum FontStyle
{
 Regular = 0;
 Bold =1;
 Italic = 2
}
// dont know what Type to cast it :/
TYPE fontstyle = FontStyle.Bold;

我不确定要强制转换哪种类型,它包含在System.Drawing类中.

I am not sure which TYPE to cast it, It is contained within System.Drawing class.

推荐答案

其类型为 FontStyle ,即枚举是一流的类型.

It's of type FontStyle i.e. Enums are first class types.

public enum FontStyle
{
    Regular = 0;
    Bold =1;
    Italic = 2
}

// No need to cast it
FontStyle fontstyle = FontStyle.Bold;

也许您有这样的代码:

if(1 == 1)
    FontStyle fontstyle = FontStyle.Bold;

对于您的错误(嵌入式语句不能是声明或标记的语句),例如,将代码包围在块语句中

for your error (Embedded statement cannot be a declaration or labeled statement) surround your code in a block statement e.g.

if(1 == 1)
{
    FontStyle fontstyle = FontStyle.Bold;
}

这篇关于如何将枚举分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 00:15