2

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?

3 Answers 3

8

Anything wrong with:

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

or

public static class RequestType { public const 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.

Sign up to request clarification or add additional context in comments.

6 Comments

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);"
@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.
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: in the first case the parameter type would just be string. In the second case it would be RequestType.
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.
|
0

How about using some kind of associative array?

1 Comment

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?
0

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!

1 Comment

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. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.