2012-08-02 4 views
1

첫 번째로,이 기능의 사용은 학습 용이며 기능 응용 프로그램에서는 사용되지 않을 것입니다.C# 사용자 지정 날짜 클래스, 문자열 형식

는 I는 Date 객체 (사용자 정의)를 수신 (DateHandler 이름) 클래스를 생성하고, 그것이 일치의에 각 문자를 변경하는 문자열를 수신하는 기능을 가지고있다. 예를 들어

:

"d/m/Y" 미리 정의되지 않는 문자가 변경되지 않습니다 "1/1/2005"

d => Day 
m => Month 
Y => Full Year 

주를 반환 할 수 있습니다.

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 
} 

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 
} 

내 문제 : 나는 각각의 새로운 DateHandler에 새 사전을 정의해야하고, 생각하려고 해요 독창적 인 방법으로, 사전 중 하나만이 일치 정의를 가리킬 것입니다. 어떤 아이디어?

내 주요 목표하십시오 DateHandler의 여러 인스턴스에서 값을 참조로 사용됩니다 사전 Properties 하나의 인스턴스입니다.

+0

명확히하기 위해 모든 항목 (예 : 'd', this.Date.Day + "")이 포함 된 사전 인스턴스 (예 : '등록 정보'사전) DateHandler '가 참조 할 것입니까? :) 만약 내가 벗어났다면 알려주세요. –

+1

'DateTime'을 저장하고 .NET 프레임 워크에 이미 존재하는 문자열 포맷 함수에 내장 된 특별한 이유가 있습니까? –

+0

@Layoric 바로. – Novak

답변

1

나는 당신이 가지고있는 것이 비합리적인 것이 아니라 항상 여러 가지 일을하는 것처럼 생각합니다. 개인적으로는 '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 
} 

이렇게하면 DateHandler를 만들 때마다 사전을 인스턴스화하지 않습니다. 귀하의 사용은 다음과 같습니다.

Date d = new Date(1, 1, 2012); 

string result = DateHandler.Format(d, "d/m/Y"); 

이는 자신의 그리기 백업하고 '도우미 클래스'를 피하기 위해 좋은 몇 번입니다,하지만 그것은 생각을위한 음식으로 도움이되기를 바랍니다.