2

We have an enum in a class library:

Public Enum FieldType
    Phone
    Span
    Gender
    DrawPath
    ....
End Enum

which we use with an attribute applied to properties, for multiple scenarios: HTML/CSV rendering, onscreen display formatting among them.

Multiple projects use this class library, and we want to use project-specific values with this enum. The class library could have extension points (e.g. Public Shared FieldTypeAsHtml As Func(Of FieldType,String)) which specific projects could override.

Each project could have its own additional enums or constants, and the conversions would be automatic (at least in VB.NET which is what we are using; C# would require explicit conversions).

Aside from the obvious -- the intention of this enum is to be "open" and not limited to a specified set of values, therefore no compile-time safety on a given value -- is this a valid design? Are there any downsides? Since there are no partial enums in .NET, what other alternatives might be better, and why?

  • This question was already discussed on Stackoverflow (more than once), see stackoverflow.com/questions/757684/enum-inheritance – Doc Brown Nov 12 '15 at 13:36
  • @DocBrown The primary thrust of my question is on the advisability/inadvisability of doing this, which is not covered by the SO question. – Zev Spitz Nov 12 '15 at 17:50
  • Is it advisable to write programs in a typesafe manner? Short answer: it depends. Sometimes yes, sometimes no, and the reasoning about this will be pretty much the same as to your question. – Doc Brown Nov 12 '15 at 18:57
3

You could use the "Smart Enum" pattern. It is intended to be extended and typesafe. Although I have C# posts about it, but I think it would be helpful anyway. See:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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