相关疑难解决方法(0)

从C#中的枚举中获取int值

我有一个叫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# int enums casting

1698
推荐指数
23
解决办法
142万
查看次数

在C#中将Int转换为Generic Enum

类似于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# generics enums casting

74
推荐指数
5
解决办法
3万
查看次数

C#int枚举转换

可能重复:
在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

54
推荐指数
4
解决办法
10万
查看次数

如何在C#中获取枚举索引值

在C中enums,内部等于整数.因此我们也可以将数据类型enum视为整数.

如何用C#实现同样的目标?

c c# enums integer

47
推荐指数
4
解决办法
11万
查看次数

如何比较枚举和int值?

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的整数字段获取数据.

.net c# enums

40
推荐指数
4
解决办法
8万
查看次数

重构类摆脱开关案例

假设我有一个这样的课程来计算不同距离的不同交通方式的旅行费用:

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)

c#

34
推荐指数
5
解决办法
7237
查看次数

使用enum表示从'int'到'type'的无效转换

在我的课上,我定义了一个这样的枚举:

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]

不要将字段映射到整数?

c++ enums

24
推荐指数
2
解决办法
4万
查看次数

是否可以将整数转换为枚举?

我得到以下枚举:

 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)

如果没有,我如何使用整数作为索引获取枚举值,我们使用了很多枚举,所以我们想做一些通用的和可重用的

.net c# casting

24
推荐指数
3
解决办法
8万
查看次数

从变量名称中获取枚举值

我在"listMovie"这样的变量中使用了枚举名称.通过使用这个我必须得到值"2"是否可能.

public enum Movies
{
    regMovie = 1,
    listMovie = 2 // member whose value I want
}
Run Code Online (Sandbox Code Playgroud)

如果任何人面临这一挑战,请提前建议...

.net c# enums

14
推荐指数
2
解决办法
2万
查看次数

如何将int转换为枚举值?

这是我的代码抛出一个错误说Cannot convert type "int" to Cards.SuitsCannot 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)

c# class

10
推荐指数
1
解决办法
1358
查看次数

标签 统计

c# ×9

enums ×7

.net ×3

casting ×3

c ×1

c++ ×1

class ×1

generics ×1

int ×1

integer ×1