刚学Enum,想把之前的int类型转成Enum类型。
发现有点难度。
代码片段如下int _classkind = 0;
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
CategoryBll bll = new CategoryBll();
switch (category)
{
case "Article":
_classkind = 0;
break;
case "Product":
_classkind = 1;
break;
}
IList<CategoryInfo> ds = bll.GetOneCate(_classkind);
_classkind 是int类型
要如何转换成Enum类型呢?主要是因为里面有个switch,根据category的值使_classkind变成0,或1。
bll.GetOneCate(_classkind);如果换成Enum应该怎么做?
发现有点难度。
代码片段如下int _classkind = 0;
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
CategoryBll bll = new CategoryBll();
switch (category)
{
case "Article":
_classkind = 0;
break;
case "Product":
_classkind = 1;
break;
}
IList<CategoryInfo> ds = bll.GetOneCate(_classkind);
_classkind 是int类型
要如何转换成Enum类型呢?主要是因为里面有个switch,根据category的值使_classkind变成0,或1。
bll.GetOneCate(_classkind);如果换成Enum应该怎么做?
{
Article,
Product
}然后 A.Article可以和int类型的0互相强制转换,A.Product可以和int类型的1互相强制转化.
{
Article = 0,
Product
}
ClassKind _classkind = ClassKind.Article;
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
CategoryBll bll = new CategoryBll(); switch (category)
{
case "Article":
_classkind = ClassKind.Article;
break;
case "Product":
_classkind = ClassKind.Product;
break;
}
IList<CategoryInfo> ds = bll.GetOneCate(Convert.ToInt32(_classkind));
还是用回以前的int类型?===================
就是结合代码片段将_classkind 转换成Enum类型
test1=1,
test2=2
}int i=1;
test t = i;
我打个比方说:
比如审核文章有3种状态。1是通过审核,2是不通过审核,3是未审核。我以前做系统的时候,是用int类型,比如1代表通过审核。来做系统。现在学了Enum,完全可以用Enum来定义这个数据类型。但这种做法和之前的int类型有什么区别?哪种效果比较好,性能方面怎样?这是我比较关心的问题。
StringBuilder strSql = new StringBuilder();
strSql.Append("select ClassId,ClassName from Category where ClassPre = '0'");
strSql.Append(" and ClassKind=" + _classkind + " ");
怎么弄? 本来是数字的变成了字符串!!!
StringBuilder strSql = new StringBuilder();
strSql.Append("select ClassId,ClassName from Category where ClassPre = '0'");
strSql.Append(" and ClassKind=" + Convert.ToInt32(_classkind).ToString() + " ");
这和你之前用
IList<CategoryInfo> ds = bll.GetOneCate(Convert.ToInt32(_classkind));的方法不同。
第一种是定义变量是枚举类型,然后在数据库操作将枚举类型转换成int类型,在bll调用的时候就不用转换类型,如bll.GetOneCate(_classkind);
第二种是定义int类型的变量,然后如果要调用的时候用枚举转换,如bll.GetOneCate(Convert.ToInt32(_classkind));哪种方案比较好呢?
{
int i=2;
student s=(student )Enum.Parse(typeof(student), i.ToString()) ;
}
}
enum student
{
name=1,
age=2 };