[英]c# Custom Date class, string format
First, I would like to say that the use of this is only for learning, and probably won't be used in a functional application. 首先,我想说的是,此功能仅用于学习,可能不会在功能性应用程序中使用。
I have created a class (named DateHandler
) that receives a Date
object (custom), and it has a function that receives a string , changing each character to it's match. 我创建了一个接收
Date
对象(自定义)的类(名为DateHandler
),它具有一个接收字符串的函数,将每个字符更改为匹配的字符。
For example: 例如:
"d/m/Y"
can return "1/1/2005"
"d/m/Y"
可以返回"1/1/2005"
d => Day
m => Month
Y => Full Year
Note that characters that aren't predefined won't be changed. 请注意,未预定义的字符不会更改。
The Date
class: Date
类:
class Date
{
#region Properties
private int _Day;
private int _Month;
public int Day
{
get
{
return _Day;
}
set
{
if (value < 1)
throw new Exception("Cannot set property Date.Day below 1");
this._Day = value;
}
}
public int Month
{
get
{
return _Month;
}
set
{
if (value < 1)
throw new Exception("Cannot set property Date.Month below 1");
this._Month = value;
}
}
public int Year;
#endregion
#region Ctors
public Date() { }
public Date(int Day, int Month, int Year)
{
this.Day = Day;
this.Month = Month;
this.Year = Year;
}
#endregion
}
The DateHandler
Class: DateHandler
类:
class DateHandler
{
#region Properties
private Date Date;
private Dictionary<char, string> Properties;
private int Properties_Count;
#endregion
#region Ctors
public DateHandler()
{
Properties = new Dictionary<char, string>();
}
public DateHandler(Date Date)
: this()
{
this.SetDate(Date);
}
#endregion
#region Methods
public void SetDate(Date Date)
{
this.Date = Date;
this.SetProperties();
}
private void SetProperties()
{
this.Properties.Add('d', this.Date.Day + "");
this.Properties.Add('m', this.Date.Month + "");
this.Properties.Add('Y', this.Date.Year + "");
this.Properties.Add('y', this.Date.Year.ToString().Substring(Math.Max(0, this.Date.Year.ToString().Length - 2)));
this.Properties_Count = Properties.Count;
}
public string Format(string FormatString)
{
int len = FormatString.Length;
if (Properties.ContainsKey(FormatString[0]))
{
FormatString = FormatString.Replace(FormatString[0] + "", this.Properties[FormatString[0]] + "");
}
for (int i = 1; i < len; i++)
{
if (this.Properties.ContainsKey(FormatString[i]) && FormatString[i - 1] != '\\')
{
FormatString = FormatString.Replace(FormatString[i] + "", this.Properties[FormatString[i]] + "");
}
}
return FormatString;
}
#endregion
}
My problem: I have to define a new dictionary to each new DateHandler
, and I'm trying to think of a creative way, that there will be only one dictionary that will point to it's match definition. 我的问题:我必须为每个新的
DateHandler
定义一个新的字典,而我正在尝试一种创造性的方法,那就是只有一个字典将指向它的匹配定义。 Any ideas how? 有什么想法吗?
My main goal: One instance of the dictionary Properties
, which will be used as a referenced to values from multiple instances of DateHandler
. 我的主要目标是:字典
Properties
一个实例,该实例将被用作DateHandler
多个实例中的值的DateHandler
。
I think what you have is not unreasonable, but as always there are multiple ways of doing things. 我认为您拥有的并非不合理,但与往常一样,有多种处理方法。 My personal preference would be to use the 'DateHandler' as a static class as a helper (since this does very little and is quite simple).
我个人的喜好是将“ DateHandler”用作静态类作为帮助器(因为这样做几乎没有,而且非常简单)。
static class DateHandler
{
#region Properties
private static Date Date;
private static Dictionary<char, string> properties;
private static Dictionary<char, string> Properties
{
get
{
if (properties == null)
{
properties = new Dictionary<char, string>();
SetProperties();
}
return properties;
}
set
{
properties = value;
}
}
private static int Properties_Count;
#endregion
#region Methods
private static void SetProperties()
{
Properties.Add('d', Date.Day + "");
Properties.Add('m', Date.Month + "");
Properties.Add('Y', Date.Year + "");
Properties.Add('y', Date.Year.ToString().Substring(Math.Max(0, Date.Year.ToString().Length - 2)));
Properties_Count = Properties.Count;
}
public static string Format(Date date, string FormatString)
{
Date = date;
int len = FormatString.Length;
if (Properties.ContainsKey(FormatString[0]))
{
FormatString = FormatString.Replace(FormatString[0] + "", Properties[FormatString[0]] + "");
}
for (int i = 1; i < len; i++)
{
if (Properties.ContainsKey(FormatString[i]) && FormatString[i - 1] != '\\')
{
FormatString = FormatString.Replace(FormatString[i] + "", Properties[FormatString[i]] + "");
}
}
return FormatString;
}
#endregion
}
This will avoid instantiating the dictionary every time you create a DateHandler. 这样可以避免在每次创建DateHandler时实例化字典。 Your use will look like this.
您的使用将如下所示。
Date d = new Date(1, 1, 2012);
string result = DateHandler.Format(d, "d/m/Y");
This has it's own draw backs and some times it's good to avoid 'helper classes', but hope it helps as food for thought. 它有其自身的缺点,有时最好避免使用“帮助类”,但希望它能为思想提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.