3
\$\begingroup\$

Which is best practice for creating an enum-like lookup for templates? I enjoy being able to bring up my templates with VS IntelliSense so either works for me.

Option 1 (enum/Dictionary combo):

public enum Template
{
    AccountInformation = 1,
    Registration = 2,
    Signed = 3
}
static readonly IDictionary<Template, String> TemplateData = new Dictionary<Template, String>()
{
    {Template.AccountInformation, "accountinfo.txt"},
    {Template.Registration, "registration.txt"},
    {Template.Signed, "signed.txt"}
};

Option 2 (static class):

public static class Template
{
    public static String AccountInformation = "accountinfo.txt";
    public static String Registration = "registration.txt";
    public static String Signed = "signed.txt";
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Option #1, but make the declaration static readonly IDictionary<> \$\endgroup\$ Apr 3, 2013 at 16:29
  • \$\begingroup\$ I have fixed the declaration, this is the option I have initially chosen so it is good to know that I may have picked the correct one. \$\endgroup\$
    – dkroy
    Apr 3, 2013 at 16:46

2 Answers 2

2
\$\begingroup\$

Try this:

public enum Template
{
    [StringValue("accountinfo.txt")]
    AccountInformation = 1,
    [StringValue("registration.txt")]
    Registration = 2,
    [StringValue("signed.txt")]
    Signed = 3
}

For more information: String Enumerations in C#

Otherwise, I prefer your second option.

\$\endgroup\$
1
  • \$\begingroup\$ Additionally I find you should name the enum EmailTemplate and its values AccountInformationEmail or RegistrationEmail or SignedEmail. \$\endgroup\$
    – t3chb0t
    Jul 18, 2016 at 9:51
1
\$\begingroup\$

I think this depends on the logical meaning of the string.

If there is a fixed number of valid values, then you should use your first option, use the enum almost everywhere, and convert to string using the dictionaly at the last possible place. (By “fixed” I mean that the string can have only a small fixed set of values in each version of the application, but the set of possible values may change between versions.)

On the other hand, if the values represent some set of “usual” values, but other values are also a possibility (e.g. a user-specified file name), the I would use option 2.

\$\endgroup\$

Your Answer

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

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