Shawn Mclean
Shawn Mclean

Reputation: 57469

Enum with object or a dictionary-like enum

I want to create a list of names and access it as a strongly typed enum. For eg.

string foo = FileName.Hello; //Returns "Hello.txt"
string foo1 = FileName.Bye; //Returns "GoodBye.doc"

Or it could be an object like:

Person p = PeopleList.Bill; //p.FirstName = "Bill", p.LastName = "Jobs"

How do I create a datatype like this?

Upvotes: 9

Views: 32694

Answers (8)

Brandon Moretz
Brandon Moretz

Reputation: 7621

You can you Extension Methods on your Enum object to return specific values.

Upvotes: 5

goalie7960
goalie7960

Reputation: 873

I would use the Description attribute to attach custom data to an enum. Then you can use this method to return the value of the description:

    public static string GetEnumDescription(Enum value)
    {
         FieldInfo fi = value.GetType().GetField(value.ToString());
         DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
         string description = (attributes.Length > 0) ? attributes[0].Description : string.Empty;
         return description;
    }

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

This is an over the top solution using attributes for your second example. Note this code has a lot of problems and is just an example.

public static T GetValue<T>(this Enum e) where T:class
{
    FieldInfo fi = e.GetType().GetField(e.ToString());
    var valueAttribute = fi.GetCustomAttributes(typeof (ValueAttribute), 
        false).FirstOrDefault() as ValueAttribute;
    if (valueAttribute != null) return valueAttribute.Value as T;
    return null;
}

class PersonValueAttribute : ValueAttribute
{
    public PersonValueAttribute(string firstName, string lastName)
    {
        base.Value = new Person {FirstName = firstName, LastName = lastName};
    }
}

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static implicit operator Person(Enum e)
    {
        return e.GetValue<Person>();
    }
}

enum PeopleList
{
    [PersonValue("Steve", "Jobs")]
    Steve
}

Allowing for simple usage:

Person steve = PeopleList.Steve;
Console.WriteLine(steve.FirstName); //Steve

Upvotes: 0

Can Gencer
Can Gencer

Reputation: 8885

You can use static class with values..

public static class PeopleList
{
   public static readonly Person Bill = new Person("Bill", "Jobs");
   public static readonly Person Joe = new Person("Joe", "Doe");
}

public static class FileNames
{
   public static readonly string Hello = "Hello.txt";
   public static readonly string Bye = "Byte.txt";
}

then you can reference them as PeopleList.Bill or FileNames.Hello. It won't have the same properties as an enum and your methods will need to take a string or Person as parameter.

Upvotes: 2

Josh M.
Josh M.

Reputation: 27783

Although the question is strange or not completely explained, here is the literal solution:

Option 1:

public static class FileName
{
    public const string Hello = "Hello.txt";
    public const string GoodBye= "GoodBye.doc";
}

Option 2:

public class Person
{
    public string FirstName {get; set; }
    public string LastName {get; set; }

    public Person(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }
}

public static class PeopleList
{
    public static Person Bill = new Person("Bill", "Jobs");
}

Upvotes: 6

Adam Maras
Adam Maras

Reputation: 26853

Here's an article on CodeProject that will show you how to create an attribute that you can apply to each enumeration member to give it some "extra" data (like your filename, in this case) that you can use elsewhere in code.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160892

just use a Dictionary<People, Person> for that:

 enum People { Bill, Bob};

 var myDict = new Dictionary<People, Person>();

 myDict.Add(People.Bill, new Person() { FirstName = "Bill", LastName = "Jobs" });

now you can get Bill back with this syntax:

Person p = myDict[People.Bill];

Upvotes: 7

Related Questions