Enum to formatted string

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?

-------------Problems Reply------------

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.)

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


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

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
// 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

Category:c# Views:0 Time:2011-03-19
Tags: c# asp.net enums

Related post

  • C# enum value as string 2010-08-09

    I have the following enum public enum Urgency { VeryHigh = 1, High = 2, Routine = 4 } To fetch an enum "value" as a string I can do this ((int)Urgency.Routine).ToString() which will return "4". Note this is different from Urgency.Routine.ToString() w

  • How to add a parameter to a formatted string function call via a #define function 2010-09-08

    I want to create macros that will insert a parameter into a function call. For example, I have function Action() declared below. Action takes as it's inputs an enum for the state number and a formatted string with optional args for the string. I want

  • Objective C / iPhone: How do I extract the actual unicode date format strings for the current region? 2010-10-01

    I am completely new to objective c and iphone development, so please be gentle (just started looking at the code for the first time tonight). According to this site: http://iphonedevelopertips.com/cocoa/date-formatter-examples.html there is a class t

  • How to interpret an Excel number format string to determine whether the value should be parsed by DateTime.FromOADate 2011-08-02

    How can I create a function "bool IsDateTime" that will reliably determine whether an Excel number format string like "[$-409]h:mm:ss AM/PM;@" indicates that the numeric value is a DateTime that should be passed to DateTime.FromOADate? I've figured o

  • C language: How to access individual format specifiers in a printf format string? 2012-02-10

    I have a format string say char *format = "hello %d world %d" and an array int array[2] = {10, 20}; with me. This is just for example sake, the number of values to be printed in the array can be arbitrary, a max-sized array with a count of the number

  • Easy way to use variables of enum types as string in C? 2008-09-29

    Here's what I am trying to do: typedef enum { ONE, TWO, THREE } Numbers; I am trying to write a function that would do a switch case similar to the following: char num_str[10]; int process_numbers_str(Numbers num) { switch(num) { case ONE: case TWO:

  • How do you put { and } in a format string 2008-10-02

    I'm trying to generate some code at runtime where I put in some boiler-plate stuff and the user is allowed to enter the actual working code. My boiler-plate code looks something like this: using System; public class ClassName { public double TheFunct

  • Cross platform format string for variables of type size_t? 2008-10-06

    On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is

  • Formatting strings in C# consistently throughout a large web application 2008-11-16

    I'm looking for a consistent way to structure my use of formatting strings throughout a large web application, and I'm looking for recommendations or best practices on which way to go. Up until now I've had a static class that does some common format

  • Custom numeric format string to always display the sign 2008-12-07

    Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it should do for zero, I'm not sure!) --------------Solutions------------- Yes, you can. There is conditional formatt

  • Protection from Format String Vulnerability 2008-12-30

    What exactly is a "Format String Vulnerability" in a Windows System, how does it work, and how can I protect against it? --------------Solutions------------- In this pseudo code the user enters some characters to be printed, like "hello" string s=get

  • How to format a Date without using code - Format String Question 2009-01-01

    How can I achieve the following with a format string: Do 01.01.2009 ? It has to work in all languages (the example would be for Germany). So there should only be the short weekday and then the short date. I tried 'ddd d' (without the '). However, thi

  • Looking for a tool to quickly test C# format strings 2009-01-09

    I am constantly forgetting what the special little codes are for formatting .NET strings. Either through ToString() or using String.Format(). Alignment, padding, month vs. minute (month is uppercase M?), abbreviation vs. full word, etc. I can never r

  • Rails logger format string configuration 2009-01-20

    How can I configure the rails logger to output its log strings in another format? I would like to get something that is more informative like: [Log Level] [Time] [Message] Debug : 01-20-2008 13:11:03.00 : Method Called This would really help me when

  • How to convert standard IP address format string to hex and long? 2009-01-29

    Does anyone know how to get the IP address in decimal or hex from standard IP address format string ("xxx.xxx.xxx.xxx")? I've tried to use the inet_addr() function but didn't get the right result. I tested it on "" the function returned

  • How to format string in XAML Setter Value? 2009-02-09

    I have learned how to format strings in the content attribute of a label like this: <Label Content="{Binding ElementName=theSlider, Path=Value}" ContentStringFormat="The font size is {0}."/> I want to do the same thing in a Setter but "ValueStr

  • Is there a format string that can be passed to the VB6 Format function that will simply group digits? 2009-02-11

    Is there a format string (?) I can pass to the VB6 Format function that will perform digit grouping? I want to replace the question mark in this statement... Debug.Print Format(123456789, "?") ...with a string that will generate the following output:

  • How to convert between "#,##0" and "{0:n2}" style format string representations? 2009-02-20

    .NET supports two types of string formatting. I'm in a situation where existing configuration data has #,##0 style formatting. A new feature requires formatting to the same output, but the API needed for this feature only accepts formatting of type {

  • How to enforce minimum width of formatted string in C# 2009-02-24

    I have the following statement DateTime now = DateTime.Now; string test = string.Format("{0}{1}{2}{3}", now.Day, now.Month, now.Year, now.Hour); This gives me: test = "242200915" But I'd like to have something like: test = "2402200915" So the questio

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.084 (s). 11 q(s)