tags:

views:

78

answers:

4

I'm pretty sure an enum isn't what I want. What I want is a list of named items

CustomerLookup = "005",
CustomerUpdate = "1010"

The "005" and "1010" aren't my values, they are the values I need to send to a 3rd party that I have no control over. There are close to 500 of them. I just want my code to look nice.

Instead of

SendRequest("005");

I'd rather see

SendRequest(RequestType.CustomerLookup);

Anyone have any self-documenting ideas without getting all crazy in the code?

A: 

How about using some kind of associative array?

kchau
I don't think I understand. If I used an associative array, then I wouldn't ever get the benefit of having Intellisense help me out. I mean, I'd still have to remember the magic strings of "CustomerLookup" and all the rest of the 500 items, right?
Matt Dawdy
+4  A: 

Anything wrong with:

public static class RequestType
{
     public static readonly string CustomerLookup = "005";
     // etc
}

? Or if you want more type safety:

public sealed class RequestType
{
     public static readonly RequestType CustomerLookup = new RequestType("005");
     // etc

     public string Code { get; private set; }

     private RequestType(string code)
     {
         this.Code = code;
     }
}

That will basically give you a fixed set of values (the constructor is private, so outside code can't create different instances) and you can use the Code property to get at the related string value.

Jon Skeet
In this case, I don't need the type safety. So, 1 class with close to 500 public static readonly string items. I guess it's not any worse than 500 values in an enum. And it would allow me to write code like "SendRequest(RequestType.CustomerLookup);"
Matt Dawdy
@Jon: Just when I was about to push my post button for the exact same static class. I know you can access SO from your dreams.
KMan
I implemented your first suggestion. It's now 3 AM and my brain has officially stopped working, because I'm getting an error of "static types cannot be used as parameters" I'm sure there is an easy way out of this, but I literally can't think constructively anymore. Thanks for the answer, and I'll puzzle out the fine details (and post what I find) tomorrow. And Jon...it's SATURDAY! :)
Matt Dawdy
@Matt: in the first case the parameter type would just be string. In the second case it would be RequestType.
Jon Skeet
Jon, perfect. Thank you. I knew I was too tired to see such a simple mistake on my part. All is working great, thank you very much.
Matt Dawdy
A: 

The way you are already doing it seems right to me.

You are clearly defining the requesttype values in your code without ambiguity and when you come to use them you have intellisense on your side.

I think the real issue is how to get the 500 values into your code without any tyypos!

CResults
Actually, I didn't say it in my original post, but the reason I asked the question is that the code I was using didn't even compile. You can't set the value of an enum member to a string. ;)
Matt Dawdy
Ahhh.. And I answered before my first coffee! Good point.
CResults
A: 

I sometimes use the extension method:

public static class EnumExtension
{
    public static string GetString(this Enum enumeration)
    {
        return Enum.GetName(enumeration.GetType(), enumeration);
    }

    public static int GetInt(this Enum enumeration)
    {
        return Convert.ToInt32(enumeration);
    }

     //if you want you can also have a type specific extension method i.e.

 public static string Code(this Enum enumeration, int characterCount){
     var code = enumeration.GetInt().ToString();
     while(code.Length<characterCount)
          code = 0.ToString()+code;

 }

}

This so this works:

   public enum RequestType{
      CustomerLookup = 5,
      CustomerUpdate=1010
   }


   public class Sample{
          public void SendRequest(RequestType requestType){
                var code = requestType.Code(4);
                 ///process 'code'
          }
   }
smartcaveman
This wouldn't work for me -- the specs call for values of "005" and "1010", etc. Not a standard length. And not predictable. I won't show you all 500 or so. Thanks for the answer, though.
Matt Dawdy