English 中文(简体)
积极改变C#中的um价值?
原标题:Dynamically change the Value of enum in C#?
  • 时间:2011-10-19 10:47:53
  •  标签:
  • c#
  • enums

我有一席之地。

    public enum TestType:int
   {
      Aphasia = 2,
      FocusedAphasia = 5
   }

具有既定的价值观。 我想将“焦点”的阿法西亚的价值从5升至10。 任何人都能够帮助我改变um的价值

最佳回答

你们不能在常年改变。 我不肯定你为什么需要,但无论如何,这是不可能的。 采用变量是另一种选择。

问题回答

如果你愿意的话,它就是一个明显类型的。

该表的内容只是读写的,不可能在操作时间加以改动,也不可能。

<>might, 适合您的是展期,以暴露新的价值,使之具有新的特征和意义,例如:

public enum TestType:int
{
    Aphasia = 2,
    FocusedAphasia = 5
    SomeOtherAphasia = 10
}

Not being quite clued up on exactly what you want to do, I can t well suggest much else.

确实,你能够这样做。 你们有一个与原测试Type的组装(批)。 你可以卸下这种组装(这种组装有些复杂),用新的测试仪把组装成并重载。

然而,你不能改变现有变量的类型,因为这些变量必须经过处理才能卸载。

这个问题是7年,而我是我的回答。 我仍然想写这段话,或许对后来的人有用。

不可能在 run时期改变 en价值,但是,通过将 variable变转化为um,将具有其价值的 in体定义为:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();
void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values
    d.Add(2,"Aphasia");
    d.Add(5,"FocusedAphasia");

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}

In this way, you have a dynamic dictionary for enum values without any written inside it. In case you want any other value for the enum, then you can make a method to add it:

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
    }
}

因此,你最后使用的法典应当这样:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
        Console.WriteLine("Addition Done!");
    }
}

void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values

    NewEnumValue(2,"Aphasia");
    NewEnumValue(5,"FocusedAphasia");
    Console.WriteLine("You will add int with their values; type 0 to " +
                      "exit");
    while(true)
    {
        Console.WriteLine("enum int:");
        int ii = Convert.ToInt32(Console.ReadLine());
        if (ii == 0) break;
        Console.WriteLine("enum value:");
        string v = Console.ReadLine();
        Console.WriteLine("will try to assign the enum TestType with " + 
                          "value: " + v + " by  " + ii + "  int value.");
        NewEnumValue(ii,v);
    }

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签