枚举到格式化字符串
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);
}
}
这是可行的,但是当我response.write时,它需要以“dd/mm/yy”格式出现,我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的答案是不要为此使用枚举。静态类怎么样?
(只是一个示例,请将字段重命名为对您有意义的任何名称。)
The simple answer is don't use an enum for this. How about a static class?
(Just a sample, rename the fields to whatever makes sense for you.)
最简单的方法是只使用
Dictionary
,您可以使用枚举的相应字符串表示来填充,即您可以这样做
Easiest way would be to just use a
Dictionary<WebWizDateFormat,string>
that you populate with corresponding string represenations for your enum, i.e.then you can just do
您关于此转换的规则不明确。你可以做类似的事情:
但我怀疑,这就是你的意思......;-)
这也可能对您有所帮助:带有用户友好字符串的枚举 ToString
Your rules regarding this conversion are not clear. You could do something like that:
But I doubt, that is what you meant... ;-)
This could also be helpful to you: Enum ToString with user friendly strings
序言:我建议不要使用枚举项名称来表示数据(您可以获取给定枚举值和类型的字符串名称)。我还建议使用隐式分配的枚举值,因为添加或删除枚举项之类的细微更改可能会产生细微的不兼容更改/错误。
在这种情况下,我可能只是创建一个从枚举值到字符串格式的映射,例如:
快乐编码
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:
Happy coding