-3

I have a best practices question. I have a method BuildTourTime that takes a DateTime and a string that represents the time.

 internal DateTime BuildDateTime(DateTime date, string time)
    {
        return date.Add(TimeSpan.Parse(time));

    }

I'll be using this in multiple locations. Currently it is being used in another class

private IEnumerable<Tour> BuildTours(Response Data)
        {
            //Tour tour = new Tour();
            List<Tour> tours = new List<Tour>();
            foreach(Events ivEvents in Data.response.events)
            {
                bool isCancelled = true; //Replace this with logic
                tours.Add(new Tour(ivEvents.Id,Data.response.ID,Data.response.Title,BuildDateTime(ivEvents.Date,ivEvent.Start),isCancelled,ivEvents.Quantity-ivEvents.Remaining).);
            }
            return tours;
        }

I currently have the Tour class without having a parameterless constructor. I have a few options here as far as i can tell. Declare the BuildDateTime static. Or add a parameterless constructor for Tour. Or Create a helper class and put BuildDateTime. Is this a good place to be using a static method? That is what i am leaning towards.

Edit: Why the downvotes? The link posted(which i read before posting this question) does not answer my question. My question shows that i have investigated options i could use and I have asked an exact question, as well as providing relative code.

Andrew MacNaughton
  • 783
  • 2
  • 6
  • 21

2 Answers2

4

You can do extension method:

internal static class DateTimeExtension
{
    internal static DateTime BuildDateTime(this DateTime date, string time)
    {
        return date.Add(TimeSpan.Parse(time));
    }
}

and use it:

string time = ...
DateTime date = ...
date.BuildDateTime(time);
rechandler
  • 756
  • 8
  • 22
  • +1, extension method makes sense for how simple the method is. I'd probably call it .AddTime instead of BuildDateTime though, just something to think about. – fooser Oct 24 '14 at 17:24
  • I like this. Is it overkill if its only going to be used in 1 project? Multiple places in 1 project – Andrew MacNaughton Oct 24 '14 at 17:31
  • @AndrewMacNaughton I don't think so ;) Is it in a project or in particular place? read this: http://msdn.microsoft.com/en-us//library/bb383977.aspx – rechandler Oct 24 '14 at 17:39
2

Your method doesn't seem to be specific for that class but could be use generically by every other classes. It seems to be the perfect example of an extension method for the DateTime structure

public static class MyDateExtensions
{
    public static DateTime BuildDateTime(this DateTime date, string time)
    {
        return date.Add(TimeSpan.Parse(time));

    }
}

in this way you could call it from everywhere (provided you add the using namespace)

// Addin a day plus one hour
DateTime newDate = DateTime.Now.BuildDateTime("01:01:00:00");

Said that, it seems a bit exagerated to use this approach when the DateTime.Add is just a single line of code and without any complexity to hide

DateTime newDate = DateTime.Now.Add(TimeSpan.Parse("01:01:00:00"));

The choice for an extension method should be preferred if you are not sure about the inputs that the parse receives. For example this could be a more safe method, but it depends a lot on what do you want to happen if the time string is invalid. Many people prefer to get the exception if the time string is invalid instead of preventing the exception and returning a predefined result.

public static DateTime BuildDateTime(this DateTime date, string time)
{
    if(string.IsNullOrWhiteSpace(time))
        return date; 

    TimeSpan ts;
    if(TimeSpan.TryParse(time, out ts))
        return date.Add(ts);
    else 
        return date;
}
Steve
  • 213,761
  • 22
  • 232
  • 286