0
public enum WebWizDateFormat
{
    DDMMYY,
    MMDDYY,
    YYDDMM,
    YYMMDD
}

 

public class WebWizForumUser
{
    public WebWizDateFormat DateFormat { get; set; }

    public WebWizForumUser()
    {
        this.DateFormat = WebWizDateFormat.DDMMYY;
        HttpContext.Current.Response.Write(this.DateFormat);
    }
}

This works, but when I response.write it needs to come out in the format "dd/mm/yy", how can I do this?

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

4 Answers4

6

The simple answer is don't use an enum for this. How about a static class?

public static class WebWizDateFormat
{
    public const string USFormat = "MM/DD/YY";
    public const string UKFormat = "DD/MM/YY";
}

// . . .
string dateFormat = WebWizDateFormat.USFormat;

(Just a sample, rename the fields to whatever makes sense for you.)

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
  • I don't really like dropping the use of enums here, but +1 because this abstracts out from MMDDYY, etc to "localized" formats. –  Mar 19 '11 at 17:15
0

Easiest way would be to just use a Dictionary<WebWizDateFormat,string> that you populate with corresponding string represenations for your enum, i.e.

DateMapping[WebWizDateFormat.DDMMYY] = "dd/mm/yy";

then you can just do

HttpContext.Current.Response.Write(DateMapping[this.DateFormat]);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

Your rules regarding this conversion are not clear. You could do something like that:

this.DateFormat.ToLower().Insert(4, "\\").Insert(2,"\\");

But I doubt, that is what you meant... ;-)
This could also be helpful to you: Enum ToString with user friendly strings

Community
  • 1
  • 1
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

Preamble: I would advise against using an enum item name to represent data (you can get the string name of a given enum value and type). I would also advise using implicitly assigned enum values as subtle changes such as adding or removing an enum item may create subtle incompatible changes/bugs.

In this case I may just create a map from an enum-value to a string format, such as:

public enum WebWizDateFormat
{
    DDMMYY = 1,
    MMDDYY = 2,
    YYDDMM = 3,
    YYMMDD = 4,
    // but better, maybe, as this abstracts out the "localization"
    // it is not mutually exclusive with the above
    // however, .NET *already* supports various localized date formats
    // which the mapping below could be altered to take advantage
    ShortUS = 10, // means "mm/dd/yy",
    LongUK = ...,
}

public IDictionary<string,string> WebWizDateFormatMap = new Dictionary<string,string> {
    { WebWizDateFormat.DDMMYY, "dd/mm/yy" },
    // "localized" version, same as MMDDYY
    { WebWizDateFormat.ShortUS, "mm/dd/yy" },
    ... // define for -all-
};

// to use later
string format = WebWizDateFormatMap[WebWizDateFormat.ShortUS];

Happy coding