C# 如何在我的表单中使用单独的枚举类
我有一个这样的函数C# 如何在我的表单中使用单独的枚举类,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我有一个这样的函数 public void SetOperationDropDown() { int ? cbSelectedValue = null; if(cmbOperations.Items.Count == 0) { //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS
public void SetOperationDropDown()
{
int ? cbSelectedValue = null;
if(cmbOperations.Items.Count == 0)
{
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
cmbOperations.SelectedItem = "-SELECT OPERATIONS-";
//This is for adding four operations with value in operation dropdown
cmbOperations.Items.Insert(0, "PrimaryKeyTables");
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
cmbOperations.Items.Insert(2, "ForeignKeyTables");
cmbOperations.Items.Insert(3, "NonForeignKeyTables");
cmbOperations.Items.Insert(4, "UPPERCASEDTables");
cmbOperations.Items.Insert(5, "lowercasedtables");
}
else
{
if(!string.IsNullOrEmpty("cmbOperations.SelectedValue"))
cbSelectedValue = Convert.ToInt32(cmbOperations.SelectedValue);
}
//Load the combo box cmbOperations again
if(cbSelectedValue != null)
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}
但是,如果我想在一个单独的枚举类中定义这个函数,然后调用它,我需要做什么呢。不太清楚您实际想要得到什么。如果希望在
enum
中定义硬编码字符串,可以这样定义:
enum Tables
{
PrimaryKeyTables,
NonPrimaryKeyTables,
ForeignKeyTables,
NonForeignKeyTables,
UPPERCASEDTables,
lowercasedtables,
}
Tables cbSelectedValue = (Tables)Enum.Parse(typeof(Tables), cmbOperations.SelectedItem.ToString());
请注意,string.IsNullOrEmpty(“cmbcoperations.SelectedValue”)代码>始终将返回false
,因为您正在测试指定的字符串。您可能希望测试以下内容:
cmbOperations.SelectedItem != null
要根据所选内容分配表
枚举,可以执行以下操作:
enum Tables
{
PrimaryKeyTables,
NonPrimaryKeyTables,
ForeignKeyTables,
NonForeignKeyTables,
UPPERCASEDTables,
lowercasedtables,
}
Tables cbSelectedValue = (Tables)Enum.Parse(typeof(Tables), cmbOperations.SelectedItem.ToString());
在单独的枚举类中是什么意思?枚举不能将函数/方法作为其成员。你可能想澄清你想要实现的目标,以便其他人能够提供适当的指导。