C#-使用类型编号代替类型名称 [英] C# - Using Type number instead of Type name

查看:85
本文介绍了C#-使用类型编号代替类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我有一个数据"类,有两种类型:ModeOne和ModeTwo

公共枚举类型{ModeOne,ModeTwo}; 

现在运行的软件正在使用所有内容的名称(保存在数据库中,进行搜索等).它在整个代码中使用:

 Data.Types.ModeOne.ToString()
Data.Types.ModeTwo.ToString()
Data.Types.ModeOne
Data.Types.ModeTwo 

它现在使用类型的名称保存所有内容,但是现在我想将其更改为类型"编号;以便它将数字全部使用.

对于前两个,我已经做到了:

公共枚举类型{ModeOne,ModeTwo};
        public static int MOne =(int)Types.ModeOne;
        public static int MTwo =(int)Types.ModeTwo; 

有了这个,我可以使用:

 Data.MOne.ToString()
Data.MTwo.ToString()

它现在搜索"0";和"1".

然而,问题在于,其他两个保持相同,并且这两个仍在搜索"ModeOne".和"ModeTwo".

我试图改变

 Data.Types.ModeOne
Data.Types.ModeTwo 

 Data.MOne
Data.MTwo 

但是随后出现错误: 

无法将类型'int'隐式转换为'OMS2.Model.Entities.Data.Types'.存在显式转换(您是否缺少演员表?)

必须更改,否则将保存"0".并搜索"ModeOne" (这将导致错误).

有人知道如何更改程序中的所有内容,使其仅保存和搜索类型编号而不是类型名称吗?显然,将其更改为"int"是不可能的.

谢谢.

Ganesh Gebhard

解决方案

您是否尝试过从 int  继承枚举并自己指定值:

公共枚举类型:int
{
   ModeOne = 1
   模式二= 2
} 



Dear all,

I have a class "Data" with two types: ModeOne and ModeTwo

public enum Types { ModeOne, ModeTwo};

The software running now is using the names for everything (saving in database, searching, etc.). It's using in the whole code:

Data.Types.ModeOne.ToString()
Data.Types.ModeTwo.ToString()
Data.Types.ModeOne
Data.Types.ModeTwo

It now uses the names of the types to save everything, but now I want to change this to the Type numbers; so that it will use the numbers for everything.

In case of the first two, I already managed to do that:

public enum Types { ModeOne, ModeTwo};
        public static int MOne= (int)Types.ModeOne;
        public static int MTwo= (int)Types.ModeTwo;

With this, I can use:

Data.MOne.ToString()
Data.MTwo.ToString()

It now searches for "0" and "1". 

The problem however is that the other two remain the same and those two are still searching for "ModeOne" and "ModeTwo".

I tried to change 

Data.Types.ModeOne
Data.Types.ModeTwo

to

Data.MOne
Data.MTwo

But then it gives the error: 

Cannot implicitly convert type 'int' to 'OMS2.Model.Entities.Data.Types'. An explicit conversion exists (are you missing a cast?)

This must change, otherwise it will save "0" and search for "ModeOne" (which will result in a error).

Does someone know how to change everything in my program in such a way that it only saves and searches for the Type numbers instead of the Type names? Changing it to 'int' doesn't work apparently. 

Thanks in advance.

Ganesh Gebhard

解决方案

Have you tried inheriting your enum fro int and specifying values yourself :

public enum Types : int 
{  
   ModeOne = 1, 
   ModeTwo = 2
}



这篇关于C#-使用类型编号代替类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆