开发者

How to update value of data type in C#

I all, my codes:

int a = 1;
int b = 2;
enum eType
{
    A,
    B
}

I have function UpdateValue to update value of a or b based on passed parameter eType:

void UpdateValue(eType type, int value)
{
switch(type)
{
   case eType.A:
      a = value;
      break;
   case eType.B:
      b = value;
      break;
}
}

And I also have function GetValue to get value of a or b based on passed parameter eType

    int GetValue(eType type)
    {
    int result = 0;
    switch(type)
    {
        case eType.A:
          result  = a;
          break;
        case eType.B:
          result  = b;
          break;
    }
    return result;
    }

This solution is very bad if I have a lot of items in enum eType. I don't like must enumurate the list of items in eType. So I have a new solution: I create a dictionary:

Dictionary<eType, int> dict = new Dictionary<eType, int>();
dict[eType.A] =a;
dict[eType.B] =b;

I easy to get value of a or b from dict and eType. Exa开发者_运维百科mple: to get value of a, I call dict[eType.A]. But how I can update value of a or b based dict and eType.

For example if I have dict[eType.A] containing the value 10

How do I set the variable a to 10.

(I want map a to eType.A, and map b to eType.B in dict. I can easy to update value of a and b from dict. I can change value of a using dict. How to do this.)

I'm using .NET 4.0.


You could try this:

enum eType: int
{
    A=0,
    B=1
    //etc...
}
int count = Enum.GetValues(typeof(eType)).Length;
int[] values = new int[count];

Then you can use values[(int)eType] both to get and set, where your variable a is values[0] and variable b is values[1]. This array can be as long as you please and you should'n add code, just the entry in eType enum...
Just an idea to work with...


You have the right idea. Here's some code that creates a dictionary and reads and updates it. I hope this will help.

var dict = new Dictionary<string, int>();
dict.Add("a", 1);
dict.Add("b", 2);
var x = dict["a"];  // 1
dict["a"] = 10;
var y = dict["a"];  // 10


If you have no control over the variables that need to be set (the primary solution would otherwise actually be to replace the variables with the dictionary itself), then I think reflection is the only way to automate this behavior at all.

A possible solution for the link between the variable and enum value would be to create a custom attribute that you can then attach to the enum values, like so:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
sealed class MemberBindingAttribute : Attribute
{
    readonly string memberName;

    public MemberBindingAttribute(string memberName)
    {
        this.memberName = memberName;
    }

    public string MemberName
    {
        get { return memberName; }
    }
}

enum eType
{
    [MemberBinding("a")]
    A,
    [MemberBinding("b")]
    B
}

And then in the UpdateValue() method query these attributes, retrieve the FieldInfo (or PropertyInfo) for the corresponding member and set the value dynamically.

Note that a reflection based solution does come with a performance cost, however.


Why not use the dictionary as your global variable and use string keys instead


You mean if I have enum for say A to Z then I would also have 26 variables from a to z and they have to be updated depending upon the value of enum.

Well this is not possible at all with the dictionary.

Reflection can be useful. If all those variables u wanted to update are fields of a class.


Take a look at generics, you can define a list of eType as the key type and int as the value type:

List<etype,int> myList = new List<etype,int>();

myList contains methods to do what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜