Enums: Good or Bad ?

T

tinman

Hi...

I'd like to some feedback as to how best to approach Enums.

For example:

Assume a staffing application which has the following enum for Project
Types:

Public Enum enmProjectType
ptDepartmental
ptDivisional
ptGeographical
End Enum

If we were to take this approach, then all is fine if the enumeration stays
the same (in an ideal world). However, in the real world, there might be
new additions to the enumeration. For example, there might now be a
ptNational - a new member of the enmProjectType.

The way I see it - the ONLY way to make the change is to modify the
enum in the source code and re-compile.

My question: how can we achieve this without compilation. Should we not use
enums at all and use, maybe, strings (taken from a combobox perhaps)
instead?
Meaning, we store these values (Departmental, Divisional etc...) in the
database
instead as enums in the code.

Cheers!
 
S

Simon Jefferies

Hi,

I would recommend retrieving the values from the database, this way you have
less to maintain/recompilation of code.

If the enumeration rarely changes then an enum could be considered, but from
what you mention it sounds that its likely to change!?

--
Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
-
 
B

Bill McCarthy

Hi tinman,

It really is a question of horses for course. Both are good approaches and it
depends on what your actual needs are. If the identifiers are only pass
through, that is, you do not write code against a particular identifier or
value, then there seems no point in using an enum for them. If however you are
actually coding against given values inside that range, then an enum makes
coding easier, safer, and more robust.

If you find yourself coding against string values, then you have taken the
wrong choice. String literals in code are the most difficult to debug and
correct, and one of the most common source of errors.

So both approaches are valid, depending on the actual requirements. If coding
against the values, choose enums, otherwise don't ;)

Bill.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top