我有一个叫Questions(复数)的类.在这个类中有一个名为Question(单数)的枚举,看起来像这样.
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
Run Code Online (Sandbox Code Playgroud)
在Questions类中,我有一个get(int foo)函数返回一个Questions对象foo.有没有一种简单的方法从枚举中获取整数值,所以我可以做类似的事情Questions.Get(Question.Role)?
类似于C#中的Cast int to enum,但我的枚举是Generic Type参数.处理这个问题的最佳方法是什么?
例:
private T ConvertEnum<T>(int i) where T : struct, IConvertible
{
return (T)i;
}
Run Code Online (Sandbox Code Playgroud)
生成编译器错误 Cannot convert type 'int' to 'T'
完整代码如下,其中value可以包含int或null.
private int? TryParseInt(string value)
{
var i = 0;
if (!int.TryParse(value, out i))
{
return null;
}
return i;
}
private T? TryParseEnum<T>(string value) where T : struct, IConvertible
{
var i = TryParseInt(value);
if (!i.HasValue)
{
return null;
}
return (T)i.Value;
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
在C#中将int转换为枚举
如果我有以下代码:
enum foo : int
{
option1 = 1,
option2,
...
}
private foo convertIntToFoo(int value)
{
// Convert int to respective Foo value or throw exception
}
Run Code Online (Sandbox Code Playgroud)
转换代码会是什么样的?
在C中enums,内部等于整数.因此我们也可以将数据类型enum视为整数.
如何用C#实现同样的目标?
enum MyEnum
{
Invalid=0,
Value1=1,
Value1=2,
}
void main ()
{
MyEnum e1 = MyEnum.Value1;
int i1 = 2;
// Is there any difference how to compare enumEration values with integers?
if ( e1==(MyEnum)i1 )... // 1st
if ( (int)e1==i1 )... // 2nd
Run Code Online (Sandbox Code Playgroud)
在每个提到的案例中,我们将enum转换为int或int转换为enum.
这些转换(性能,还是其他)有什么不同吗?或者他们是完全一样的?
谢谢.
PS在当前的例子中我比较'魔术数'但在实际应用中我从DB的整数字段获取数据.
假设我有一个这样的课程来计算不同距离的不同交通方式的旅行费用:
public class TransportationCostCalculator
{
public double DistanceToDestination { get; set; }
public decimal CostOfTravel(string transportMethod)
{
switch (transportMethod)
{
case "Bicycle":
return (decimal)(DistanceToDestination * 1);
case "Bus":
return (decimal)(DistanceToDestination * 2);
case "Car":
return (decimal)(DistanceToDestination * 3);
default:
throw new ArgumentOutOfRangeException();
}
}
Run Code Online (Sandbox Code Playgroud)
这很好,但是开关箱可能是维护明智的噩梦,如果我想稍后使用飞机或火车怎么办?然后我必须改变上面的课程.我可以在这里使用什么替代开关盒以及如何提示?
我想象在这样的控制台应用程序中使用它,它将从命令行运行,包含你想要使用什么样的运输工具的参数,以及你想要旅行的距离:
class Program
{
static void Main(string[] args)
{
if(args.Length < 2)
{
Console.WriteLine("Not enough arguments to run this program");
Console.ReadLine();
}
else
{
var transportMethod = args[0];
var distance = args[1];
var calculator = …Run Code Online (Sandbox Code Playgroud) 在我的课上,我定义了一个这样的枚举:
class myClass
{
public:
enum access {
forL,
forM,
forA
};
typedef access AccessType;
AccessType aType;
};
Run Code Online (Sandbox Code Playgroud)
后来定义了这样一个对象:
myClass ob;
ob->aType = 0;
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误:
error: invalid conversion from 'int' to 'myClass::AccessType {aka myClass::access}' [-fpermissive]
不要将字段映射到整数?
我得到以下枚举:
public enum detallistaDocumentStatus {
/// <remarks/>
ORIGINAL,
/// <remarks/>
COPY,
/// <remarks/>
REEMPLAZA,
/// <remarks/>
DELETE,
}
Run Code Online (Sandbox Code Playgroud)
然后我得到了detallistaDocumentStatus类的类属性:
public detallistaDocumentStatus documentStatus {
get {
return this.documentStatusField;
}
set {
this.documentStatusField = value;
}
}
Run Code Online (Sandbox Code Playgroud)
在现实生活中,用户将向我们发送一个数字(1,2,3或4),按照声明的顺序表示每个枚举值.
那么,有可能像这样投射吗?
det.documentStatus = (detallistaDocumentStatus)3;
Run Code Online (Sandbox Code Playgroud)
如果没有,我如何使用整数作为索引获取枚举值,我们使用了很多枚举,所以我们想做一些通用的和可重用的
我在"listMovie"这样的变量中使用了枚举名称.通过使用这个我必须得到值"2"是否可能.
public enum Movies
{
regMovie = 1,
listMovie = 2 // member whose value I want
}
Run Code Online (Sandbox Code Playgroud)
如果任何人面临这一挑战,请提前建议...
这是我的代码抛出一个错误说Cannot convert type "int" to Cards.Suits和
Cannot convert type "int" to Cards.Rank
private Card[] cards;
public Deck()
{
cards = new Card[52];
for (int suitVal = 0; suitVal < 4; suitVal++)
{
for (int rankVal = 0; rankVal < 14; rankVal++)
{
cards[suitVal * 13 + rankVal - 1] = new Card((Suits)suitVal, (Rank)rankVal);
}
}
}
Run Code Online (Sandbox Code Playgroud)
卡构造函数是
public readonly Suits suit;
public readonly Rank rank;
public Card(Suits newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
} …Run Code Online (Sandbox Code Playgroud)