2

Suppose in a manufacturing environment, there are certain stock materials available for use in a product. For example, there are only a few different sizes of copper tube, each having a specific thickness and diameter combination. For the purposes of clean and minimal code as well as the ability to control the available options, is an enum a good fit for this combination of data? I was thinking I could make an enum with a member for each valid stock item (representing metal, physical measurements, etc.), and then provide extension methods on the enum to return the mentioned details.

The stocks available for use may change over time, so maybe an enum is not the best answer. Is there a better way to express the valid combination of related values? I am using c#.

1

2 Answers 2

4

I would probably just make a class TubeParameterCombination with read-only properties Thickness and Diameter, and provide an array or list like

List<TubeParameterCombination> validCombinations;

somewhere in the code. If you want to be absolutely sure no one changes that list afterwards once it got initialized, encapsulate it behind some kind of ValidCombinationProviderclass which allows only read-only access. What you have to decide is where and when you initialize that list.

You can either hardcode the initialization in your program (which means you have to change your program whenever the valid combinations change), read the combinations from a text file (which could be changed by a user ot an administrator), maybe just once when your program starts. Or you read the valid combinations from a database (which makes sense when your program is already using a database for such kind of data, and you want to provide a possibility for controlled changes to that list). Pick whatever suits your needs best.

3
  • This is along the line of what I started to think about once I realized enum wasn't the best idea. This is similar to the Typesafe Enum pattern that Rory mentioned in the question comments. I've marked this as the answer because it provided some other good notes as well. I'm now looking into what all I will need to do for this to be serializable.
    – Ethan
    Jan 30, 2014 at 16:35
  • @Ethan: "Typesafe Enum" is only the "hardcoded" version of my suggestion, with some mechanics prohibiting the creation of new objects after initialization.
    – Doc Brown
    Jan 30, 2014 at 16:41
  • gotcha, that makes sense. I see the distinction now. I'm planning to dynamically read in the valid combinations as you suggest, and expose them through a static property there on the Combination class.
    – Ethan
    Jan 30, 2014 at 16:44
0

Enum in C# is meant as list of static enough items to discriminate something. If there is even chance of this list of items changing, especially in production, then using it is not appropriate. If you want specific behavior, you have to code it yourself. It shouldn't be hard to code a class or hierarchy of classes that express what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.