Question

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.

For example:

"d/m/Y" can return "1/1/2005"

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

Note that characters that aren't predefined won't be changed.

The Date class:

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:

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

Was it helpful?

Solution

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top