views:

81

answers:

2

I need to translate result strings from a library into exceptions. Each string has a numeric result code, followed by a pipe char, and then additional, code specific data. I'm thinking of using a custom exception with a ResultCode property, and storing a lookup table of message strings keyed by result code, which I will format with an array of message infos before throwing the exception. What is a good way to store this table of int-string values?

+1  A: 

If you need to store these strings which you already have, you should use a simple Dictionary<int,string>, where the errorcode is the key and the string is the value.

If you type the strings yourself, you should consider using a Resources file.

configurator
+1  A: 

Where the data used to make the decision on which error to raise is never used outside of a single class, the optimal approach is to actually hard code the decision. That is what application programming is all about, vs. systems or frameworks programming. I suffer enough narcissism without imagining myself the divine provider of a perfect business programming framework to recognise that sometimes, my code will only be used in the one place I develop it in.

Therefore, I developed two candidate solutions to this problem: 1. The suggested dictionary of strings (codes) as keys to various exception types, initialised in a constructor; or 2. A medium sized 'switch' statement, with the same hard-coding as the constructor above.

In a single use scenario that is very likely to remain single use for the lifespan of the application, either of the above is a more than just acceptable solution; it is a cost effective, YAGNI based solution that allows me to devote more energy to problems that actually require attention.

ProfK