tags:

views:

220

answers:

10

I am writing a Movie class that will have a Year property. Should it be just an int, or should I use a DateTime object?

Just wondering the best option. Maybe I am missing something.

+14  A: 

I would probably use an int for simplicity, and make sure that in the setter you verify that the year value makes sense.

Alternatively, you can create a type to just represents years - this would make sure you don't misuse the year as a regular integral value. This gets complicated though, especially if you want to start overloading operators to support year addition and subtraction. Unless you really need this extra level of type safety, I would stick with an int.

LBushkin
This is consistent with the way a year is represented by `System.DateTime`. If you are quantifying time, then best use a `System.TimeSpan` or an `System.Globalization.Calendar`.
Steve Guidi
+1  A: 

use datetime object you can get the year from it like .year well....

abmv
+6  A: 

If it's only going to be a year value, then int will be simpler. You could also consider just saving the release date as a DateTime, and get the year from that (instead of having a year attribute).

Kaleb Brasee
That's the answer I was about to post. Use an Int unless you also keep track of a release date.
Meta-Knight
THanks, good point.
Joan Venge
+1  A: 

Well, DateTime has the unfortunate side-effect of specifying both a data and a time and not only some subsets. Ideally you would probably want some "time" object with varying levels of accuracy, as needed. But for this I'd suggest using an int since you are modeling exactly a year, not a complete date.

Joey
A: 

I think to properly answer that question, you need to supply a little more context. For what kind of application? Is this information going to be stored in a database? What kinds of queries do you expect users to perform against the data? Things like that.

Tim Keating
Thansk, it's a movie collection application. So the year will be in an xml already present that I will read and give the option to sort movies by year, or show movies from a certain year range, etc.
Joan Venge
A: 

If you're only looking to keep track of the Year, then keep it a int. If you're tracking the release date/production date, then use DateTime

Agent_9191
+1  A: 

I would create a custom type, (a struct) to hold this value.

public struct FilmYear
{
   private int yr;
   private bool isDef;
   public bool HasValue { return isDef; }
   public bool IsNull { return !HasValue; }
   private FilmYear(int year) { yr = year; isDef = true; }

   public static FilmYear ThisYear = new FilmYear(DateTime.Today.Year);
   public static FilmYear LastYear = new FilmYear(DateTime.Today.Year - 1);
   public static FilmYear NextYear = new FilmYear(DateTime.Today.Year + 1);
   public static FilmYear Parse(DateTime anyDateInYear)
   { return new FilmYear(anyDateInYear.Year); }
   public static FilmYear Parse(int year)
   { return new FilmYear(year); }
   public static FilmYear Parse(string year)
   { return new FilmYear(Int32.parse(year)); }
   public overide string ToString()
   { return yr.ToString(); }
   //etc... you can add: 
   //  - operator overloads to add subtract years to the value,
   //  - conversion operator overloads to implicitly/(or explicitly) 
   //    convert datetimes to FilmYears, as appropriate
   //  - overload equality and comparison operators ... 
}

Usage

 FilmYear avatarYear = FilmYear.ThisYear; 
 FilmYear casablancaYear = FilmYear.Parse(1943); 
Charles Bretana
Thanks out of curiosity, why didn't you make a public constructor?
Joan Venge
Two reasons.. 1) It standardizes the access syntax to more consistently resemble that of an enum, (through static factory methods) and 2) to force all initializations to go through that factory method... In this simplified example there is little impact, but as this structure becomes more complex, it allows a single common point of entry to be more easily managed.
Charles Bretana
+4  A: 

If it's only ever going to be the year then an int (or custom type) would do.

If you want to store the month as well then I'd use a DateTime.

The Agile mantra - YAGNI (You Ain't Gonna Need It) - would suggest an int until you want more information and then refactor into a DateTime then.

ChrisF
Thanks by deriving from an int, can you really do this? I didn't know you could have a type that inherits from Int.
Joan Venge
@Joan Venge - you're right. `int` is a `sealed` class. Answer updated.
ChrisF
Hehe, thanks....
Joan Venge
A: 

For simplicity using an int is the most direct option; if there are any special methods for the data then creating a Year type and encapsulating all behaviors would be a clean and simple solution--if you go that route you can store the value as either an int or DateTime and offer .ToInt(), .ToDateTime(), methods and others to handle all use-cases.

STW
+1  A: 

If this is for casual users, an int (or class based on int) is correct.

If you are doing a 'real' filmography, you'll need both an int and a string: the int for sorting and searching, with the string containing the "truth" of cases where the data is incomplete or tentative ("1958?").

This is also why you shouldn't use a Date or DateTime: there's no way to distinguish between "1/1/1958" and "sometime in 1958".

egrunin