0

I have a large collection of data, i need to store the same in string array.

this array then passed into another class as callback event.

so i need to use index as enum values so that outside person can read the string values easily.

for this i tried this,

public enum PublicData : int 
{
  CardNo = 0,
  CardName = 1,
  Address = 2,
   //etc....
}

then i have accessed this by,

string[] Publicdata = new string[iLength];
Publicdata[PublicData.CardNo] =  cardNovalue;

but here i am getting "invalid type for index - error"

How to resolve the same.

Jules
  • 537
  • 1
  • 6
  • 15
Abu Muhammad
  • 271
  • 1
  • 12

3 Answers3

3

Create custom collection class and add indexer property with PublicData enum as indexer.

public enum PublicData : int
{
    CardNo = 0,
    CardName = 1,
    Address = 2,
}

public class PublicdataCollection
{
    private readonly string[] _inner;

    public string this[PublicData index]
    {
        get { return _inner[(int)index]; }
        set { _inner[(int) index] = value; }
    }

    public PublicdataCollection(int count)
    {
        _inner = new string[count];
    }
}
user1681317
  • 188
  • 1
  • 10
  • Looked for an easy or inbuilt c# answer for this. but this seems to converting to int inside the class, and i don't know about the performance if i called this inside the thread routine. finally just converting to int better than this since i need code performance. thanks for pinging. – Abu Muhammad Nov 17 '16 at 09:44
0

Or you can go with an Extension method:

public static class MyEnumExtensions
{
    public static int Val(this PublicData en)
    {
        return (int)en;            
    }
}

Usage:

Publicdata[PublicData.Address.Val()];
Mitulát báti
  • 1,178
  • 3
  • 15
  • 30
0

Just cast your enum value

Publicdata[(int)PublicData.CardNo] =  cardNovalue;
MirrorBoy
  • 400
  • 4
  • 8