【发布时间】:2011-06-27 00:56:40
【问题描述】:
我有一个包含常量字符串的类。我想将所有这些字符串放入下拉集合中。做这个的最好方式是什么?这就是我现在所拥有的,理论上,我认为这将是做到这一点的最佳方式。
public class TestClass
{
private const string _testA = "Test A";
private const string _testB = "Test B";
public string TestA
{
get { return _testA; }
}
public string TestB
{
get { return _testB; }
}
}
public DropDownItemCollection TestCollection
{
DropDownItemCollection collection = new DropDownItemCollection();
TestClass class = new TestClass();
foreach (string testString in class)
{
DropDownItem item = new DropDownItem();
item.Description = testString;
item.Value = testString;
collection.Add(item);
}
return collection;
}
问题在于这会在 foreach 上返回错误:“...不包含 GetEnumerator 的公共定义。”我尝试创建一个 GetEnumerator,但没有成功,而且我过去没有使用过 GetEnumerator。
非常感谢任何帮助!
【问题讨论】: