【发布时间】:2015-01-05 23:37:28
【问题描述】:
所以我必须将一个类中的对象初始化到主窗体中,但我必须放入构造函数中的参数来自我在该类中创建的枚举类型。
private List<Geluidsfragment> fragmenten;
private enum ThemaSoorten
{
Muziek,
Luisterboeken,
Cabaretshows
}
^按照建议,这部分现在在课堂之外。
// Constrcutors
public BGExperience(ThemaSoorten thema)
{
fragmenten = new List<Geluidsfragment>();
this.thema = thema;
}
这些是字段和字段的构造函数。下面是我需要的这个类的对象的初始化。
public GeluidsfragmentForm()
{
InitializeComponent();
BGExperience bgExperience = new BGExperience("Muziek");
}
所以重载必须是 ThemaSoorten 类型,并且必须在枚举中,但此时它卡住了。
有人知道怎么解决吗?
【问题讨论】:
-
BGExperience bgExperience = new BGExperience(ThemaSoorten.thema1);
-
你需要将枚举声明为
public并且可能在类之外。 -
不能像这样为枚举值指定字符串值,只能指定数值。为什么不使用
public enum ThemaSoorten { Muziek, Luisterboeken, Cabaretshows };? -
您不能将字符串分配给
enum值(至少在 C# 4.0 中没有)。 MSDN 声明“已批准的枚举类型为 byte、sbyte、short、ushort、int、uint、long 或 ulong。”
标签: c# constructor enums