How to implement Year for movies in C#? - c#

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.

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.

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

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.

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

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.

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

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

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.

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

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.

Related

Objects with many value checks c#

I want to see your ideas on a efficient way to check values of a newly serialized object.
Example I have an xml document I have serialized into an object, now I want to do value checks. First and most basic idea I can think of is to use nested if statments and checks each property, could be from one value checking that it has he correct url format, to checking another proprieties value that is a date but making sue it is in the correct range etc.
So my question is how would people do checks on all values in an object? Type checks are not important as this is already taken care of it is more to do with the value itself. It needs to be for quite large objects this is why I did not really want to use nested if statements.
Edit:
I want to achieve complete value validation on all properties in a given object.
I want to check the value it self not that it is null. I want to check the value for specific things if i have, an object with many properties one is of type string and named homepage.
I want to be able to check that the string in the in the correct URL format if not fail. This is just one example in the same object I could check that a date is in a given range if any are not I will return false or some form of fail.
I am using c# .net 4.
Try to use Fluent Validation, it is separation of concerns and configure validation out of your object
public class Validator<T>
{
List<Func<T,bool>> _verifiers = new List<Func<T, bool>>();
public void AddPropertyValidator(Func<T, bool> propValidator)
{
_verifiers.Add(propValidator);
}
public bool IsValid(T objectToValidate)
{
try {
return _verifiers.All(pv => pv(objectToValidate));
} catch(Exception) {
return false;
}
}
}
class ExampleObject {
public string Name {get; set;}
public int BirthYear { get;set;}
}
public static void Main(string[] args)
{
var validator = new Validator<ExampleObject>();
validator.AddPropertyValidator(o => !string.IsNullOrEmpty(o.Name));
validator.AddPropertyValidator(o => o.BirthYear > 1900 && o.BirthYear < DateTime.Now.Year );
validator.AddPropertyValidator(o => o.Name.Length > 3);
validator.Validate(new ExampleObject());
}
I suggest using Automapper with a ValueResolver. You can deserialize the XML into an object in a very elegant way using autommaper and check if the values you get are valid with a ValueResolver.
You can use a base ValueResolver that check for Nulls or invalid casts, and some CustomResolver's that check if the Values you get are correct.
It might not be exacly what you are looking for, but I think it's an elegant way to do it.
Check this out here: http://dannydouglass.com/2010/11/06/simplify-using-xml-data-with-automapper-and-linqtoxml
In functional languages, such as Haskell, your problem could be solved with the Maybe-monad:
The Maybe monad embodies the strategy of combining a chain of
computations that may each return Nothing by ending the chain early if
any step produces Nothing as output. It is useful when a computation
entails a sequence of steps that depend on one another, and in which
some steps may fail to return a value.
Replace Nothing with null, and the same thing applies for C#.
There are several ways to try and solve the problem, none of them are particularly pretty. If you want a runtime-validation that something is not null, you could use an AOP framework to inject null-checking code into your type. Otherwise you would really have to end up doing nested if checks for null, which is not only ugly, it will probably violate the Law of Demeter.
As a compromise, you could use a Maybe-monad like set of extension methods, which would allow you to query the object, and choose what to do in case one of the properties is null.
Have a look at this article by Dmitri Nesteruk: http://www.codeproject.com/Articles/109026/Chained-null-checks-and-the-Maybe-monad
Hope that helps.
I assume your question is: How do I efficiently check whether my object is valid?
If so, it does not matter that your object was just deserialized from some text source. If your question regards checking the object while deserializing to quickly stop deserializing if an error is found, that is another issue and you should update your question.
Validating an object efficiently is not often discussed when it comes to C# and administrative tools. The reason is that it is very quick no matter how you do it. It is more common to discuss how to do the checks in a manner that is easy to read and easily maintained.
Since your question is about efficiency, here are some ideas:
If you have a huge number of objects to be checked and performance is of key importance, you might want to change your objects into arrays of data so that they can be checked in a consistent manner. Example:
Instead of having MyObject[] MyObjects where MyObject has a lot of properties, break out each property and put them into an array like this:
int[] MyFirstProperties
float[] MySecondProperties
This way, the loop that traverses the list and checks the values, can be as quick as possible and you will not have many cache misses in the CPU cache, since you loop forward in the memory. Just be sure to use regular arrays or lists that are not implemented as linked lists, since that is likely to generate a lot of cache misses.
If you do not want to break up your objects into arrays of properties, it seems that top speed is not of interest but almost top speed. Then, your best bet is to keep your objects in a serial array and do:
.
bool wasOk = true;
foreach (MyObject obj in MyObjects)
{
if (obj.MyFirstProperty == someBadValue)
{
wasOk = false;
break;
}
if (obj.MySecondProperty == someOtherBadValue)
{
wasOk = false;
break;
}
}
This checks whether all your objects' properties are ok. I am not sure what your case really is but I think you get the point. Speed is already great when it comes to just checking properties of an object.
If you do string compares, make sure that you use x = y where possible, instead of using more sophisticated string compares, since x = y has a few quick opt outs, like if any of them is null, return, if the memory address is the same, the strings are equal and a few more clever things if I remember correctly. For any Java guy reading this, do not do this in Java!!! It will work sometimes but not always.
If I did not answer your question, you need to improve your question.
I'm not certain I understand the depth of your question but, wouldn't you just do somthing like this,
public SomeClass
{
private const string UrlValidatorRegex = "http://...
private const DateTime MinValidSomeDate = ...
private const DateTime MaxValidSomeDate = ...
public string SomeUrl { get; set; }
public DateTime SomeDate { get; set; }
...
private ValidationResult ValidateProperties()
{
var urlValidator = new RegEx(urlValidatorRegex);
if (!urlValidator.IsMatch(this.Someurl))
{
return new ValidationResult
{
IsValid = false,
Message = "SomeUrl format invalid."
};
}
if (this.SomeDate < MinValidSomeDate
|| this.SomeDate > MinValidSomeDate)
{
return new ValidationResult
{
IsValid = false,
Message = "SomeDate outside permitted bounds."
};
}
...
// Check other fields and properties here, return false on failure.
...
return new ValidationResult
{
IsValid = true,
};
}
...
private struct ValidationResult
{
public bool IsValid;
public string Message;
}
}
The exact valdiation code would vary depending on how you would like your class to work, no? Consider a property of a familar type,
public string SomeString { get; set; }
What are the valid values for this property. Both null and string.Empty may or may not be valid depending on the Class adorned with the property. There may be maximal length that should be allowed but, these details would vary by implementation.
If any suggested answer is more complicated than code above without offering an increase in performance or functionality, can it be more efficient?
Is your question actually, how can I check the values on an object without having to write much code?

Reconciling properties of structs and DateTime

I have been looking through the DateTime structure and I am slightly confused.
My understanding with structs is that you cannot assign 'default values' of fields. If the default constructor of a struct is used (which is not something that you can control), then any fields will be initialized using the default values of their value type.
This is all good and well, but why then is the default value of the 'Days' property of a DateTime equal to 1? How do they pull that off?
William
You need to understand the difference between fields and properties.
The fields are all initialized to 0, but the properties can do what they like with those fields. Sample:
public struct Foo
{
private readonly int value;
public Foo(int value)
{
this.value = value;
}
public int ValuePlusOne { get { return value + 1; } }
}
...
Foo foo = new Foo(); // Look ma, no value! (Defaults to 0)
int x = foo.ValuePlusOne; // x is now 1
Now obviously DateTime is a smidge more complicated than this, but it gives the right idea :) Imagine what "A DateTime with the field explicitly set to 0" would mean... the "default" DateTime just means exactly the same thing.
Jon Skeet is right it's all about the difference between fields and other members. One could really make a "date time" like this:
struct MyDateTime
{
// This is the only instance field of my struct
// Ticks gives the number of small time units since January 1, 0001, so if Ticks is 0UL, the date will be just that
readonly ulong Ticks;
// here goes a lot of instance constructors,
// get-only instance properties to show (components of) the DateTime in a nice way,
// static helper methods,
// and lots of other stuff, but no more instance fields
...
}
So in reality, MyDateTime is just a wrapped ulong with an interpretation, and a lot of nice ways to show and manipulate that ulong.

How can i sort by a string property but not alphabetically

i have an object that has a string property called "Status". I have a collection of these objects and want to order a collection by that field which is a string.
the possible values of Status are:
"Open", "Closed", "Pending" so i can't use alphabetic order. What is the best way to say that "Open" is greater than "Closed" which is greater than "Pending" ?
I know there is an IComparer<> interface but i want to see the most elegant way to define the order of various strings.
How about using an Enum instead of a string?
public enum Status
{
Pending,
Closed,
Open
}
Then Open > Closed > Pending
You can also use Jon Skeet's answer to convert your string to an enum, if you can't change the fact that you're returning string.
I would create a mapping of some form, from the string to some naturally sortable value (e.g. integer).
Then you can use:
var sorted = unsorted.OrderBy(x => ConvertStatusToInt32(x.Status));
The conversion method might use a statically-initialized Dictionary, or a switch, or whatever.
(You might want to consider using an enum, by the way - ideally for your original status. That way you don't need to rely on strings at all.)
I think your problem here is that your trying to force semantics on to the string class. If I were you, I'd create my own status object:
public class Status : IComparable
{
private string statusText_ = string.Empty;
private int priority_ = 0;
public class Status(string text, int priority)
{
statusText_ = text;
priority_ = priority;
}
// accessors ...
public int CompareTo(object o)
{
// add compare logic to compare priorities here.
}
}
You could define the order here:
string[] bestOrder = new string[] { "Open", "Closed", "Pending" };
Then sort by the index of (i.e. IndexOf) the string in question in bestOrder.
Depending on how you are sorting the code will vary.. but a delegate to do x => bestOrder.IndexOf(x) or similar is probably what you want.
If it's a static finite collection of options you could replace the string with an enumeration and sort on the integer value of the enumeration .

Casting string value to enumerated value C#

I have a class MyClass which has
public enum Days{Mon, Tue, Wed}
and then a field
public Days dayOfWeek;
From another class in my solution I have a string (myString) value of either 0, 1 or 2. I want to set an instance of MyClass's (called myClassInstance) field dayOfWeek equal to myStringvalue such that 0 means Mon, 1 means Tue...
I have tried
myClassInstance.dayOfWeek = Convert.ToInt32(myString)
and
myClassInstance.dayOfWeek = (int) myString
but neither work. I'm sure this is straightforward. Why don't these techniques work?
Try
string s = "0";
Days day = (Days)Enum.Parse(typeof(Days), s);
or
string s = "0";
Days day;
if(!Enum.TryParse(s, out day)) {
// error handling
}
to gracefully handle the case where s can't be parsed to an instance of Days.
This works per the documentation for Enum.Parse which states
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
Additionally, you can check if the instance of string actually represents a value defined by the enum via
string s = "3";
bool defined = Enum.IsDefined(typeof(Days), s);
// defined is false
Enum.Parse will blindly parse s in this case, even though it doesn't represent a value defined by the enum Days and the cast from the result of Enum.Parse to Days will not fail.
Moreover, there is a built-in enum that represents the days of the week. This enum is System.DayOfWeek. I would suggest using this.
Finally, if for some reason you can't use System.DayOfWeek, you should at a minimum rename your enum to Day instead of Days (remove the pluralization). Only enums that represent flags should be pluralized. Note that the variable day above represents a day, and it does not represent days. This is why you should rename the enum to Day. This is consistent with the naming conventions that most C# and .NET programmers use.
You just need to cast the int to the Days enum after converting it:
myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);
You can also use Enum.TryParse (if you're in .NET 4) or Enum.Parse. Depending on how much your trust the incoming data, you may also want to call Enum.IsDefined to make sure that the integer is a valid value of Days (otherwise, in all of these cases, you'll have an instance of Days that doesn't correspond to any of your named values).
Days dayOfWeek;
if (!Enum.TryParse(myString, out dayOfWeek)) {
dayOfWeek = Days.Mon; // or some other default, or throw
}
myClassInstance.dayOfWeek = dayOfWeek;
Or
myClassInstance.dayOfWeek = (Days)Enum.Parse(typeof(Days), myString);
In addition, as others have mentioned, you may want to consider using the built-in DayOfWeek enum instead of your custom version, if it matches what you really want.
Also, as others have mentioned again, even if it doesn't, Day is a better name based on the .NET naming guidelines, since it isn't a Flags enum.
Try
myClassInstance.dayOfWeek = (Days)int.Parse(myString);
Do you know there is already an Enum that you can resuse called DayOfWeek (http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx)
Try:
Days d = (Days)Enum.Parse(typeof(Days), myString);
A more complete example is here.
You need to cast the int to Days:
myClassInstance.dayOfWeek = (Days)Convert.ToInt32(myString);
What you want to do is cast the string to an int (or tryparse if you want to do it in a nice way)
Then use the Following code to set the enum value:
(Days)Enum.ToObject(typeof(Days), intValue);
This should work
myClassInstance.dayOfWeek = Enum.Parse(typeof(Days), myString);
Enump.Parse

Extending an enum via inheritance

I know this rather goes against the idea of enums, but is it possible to extend enums in C#/Java? I mean "extend" in both the sense of adding new values to an enum, but also in the OO sense of inheriting from an existing enum.
I assume it's not possible in Java, as it only got them fairly recently (Java 5?). C# seems more forgiving of people that want to do crazy things, though, so I thought it might be possible some way. Presumably it could be hacked up via reflection (not that you'd every actually use that method)?
I'm not necessarily interested in implementing any given method, it just provoked my curiosity when it occurred to me :-)
The reason you can't extend Enums is because it would lead to problems with polymorphism.
Say you have an enum MyEnum with values A, B, and C , and extend it with value D as MyExtEnum.
Suppose a method expects a myEnum value somewhere, for instance as a parameter. It should be legal to supply a MyExtEnum value, because it's a subtype, but now what are you going to do when it turns out the value is D?
To eliminate this problem, extending enums is illegal
You're going the wrong way: a subclass of an enum would have fewer entries.
In pseudocode, think:
enum Animal { Mosquito, Dog, Cat };
enum Mammal : Animal { Dog, Cat }; // (not valid C#)
Any method that can accept an Animal should be able to accept a Mammal, but not the other way around. Subclassing is for making something more specific, not more general. That's why "object" is the root of the class hierarchy. Likewise, if enums were inheritable, then a hypothetical root of the enum hierarchy would have every possible symbol.
But no, C#/Java don't allow sub-enums, AFAICT, though it would be really useful at times. It's probably because they chose to implement Enums as ints (like C) instead of interned symbols (like Lisp). (Above, what does (Animal)1 represent, and what does (Mammal)1 represent, and are they the same value?)
You could write your own enum-like class (with a different name) that provided this, though. With C# attributes it might even look kind of nice.
When built-in enums aren't enough, you can do it the old fashion way and craft your own. For example, if you wanted to add an additional property, for example, a description field, you could do it as follows:
public class Action {
public string Name {get; private set;}
public string Description {get; private set;}
private Action(string name, string description) {
Name = name;
Description = description;
}
public static Action DoIt = new Action("Do it", "This does things");
public static Action StopIt = new Action("Stop It", "This stops things");
}
You can then treat it like an enum like so:
public void ProcessAction(Action a) {
Console.WriteLine("Performing action: " + a.Name)
if (a == Action.DoIt) {
// ... and so on
}
}
The trick is to make sure that the constructor is private (or protected if you want to inherit), and that your instances are static.
Enums are supposed to represent the enumeration of all possible values, so extending rather does go against the idea.
However, what you can do in Java (and presumably C++0x) is have an interface instead of a enum class. Then put you standard values in an enum that implements the feature. Obviously you don't get to use java.util.EnumSet and the like. This is the approach taken in "more NIO features", which should be in JDK7.
public interface Result {
String name();
String toString();
}
public enum StandardResults implements Result {
TRUE, FALSE
}
public enum WTFResults implements Result {
FILE_NOT_FOUND
}
You can use .NET reflection to retrieve the labels and values from an existing enum at run-time (Enum.GetNames() and Enum.GetValues() are the two specific methods you would use) and then use code injection to create a new one with those elements plus some new ones. This seems somewhat analagous to "inheriting from an existing enum".
I didn't see anyone else mention this but the ordinal value of an enum is important. For example, with grails when you save an enum to the database it uses the ordinal value. If you could somehow extend an enum, what would be the ordinal values of your extensions? If you extended it in multiple places how could you preserve some kind of order to these ordinals? Chaos/instability in the ordinal values would be a bad thing which is probably another reason why the language designers have not touched this.
Another difficulty if you were the language designer, how can you preserve the functionality of the values() method which is supposed to return all of the enum values. What would you invoke this on and how would it gather up all of the values?
Adding enums is a fairly common thing to do if you go back to the source code and edit, any other way (inheritance or reflection, if either is possible) is likely to come back and hit you when you get an upgrade of the library and they have introduced the same enum name or the same enum value - I have seen plenty of lowlevel code where the integer number matches to the binary encoding, where you would run into problems
Ideally code referencing enums should be written as equals only (or switches), and try to be future proof by not expecting the enum set to be const
If you mean extends in the Base class sense, then in Java... no.
But you can extend an enum value to have properties and methods if that's what you mean.
For example, the following uses a Bracket enum:
class Person {
enum Bracket {
Low(0, 12000),
Middle(12000, 60000),
Upper(60000, 100000);
private final int low;
private final int high;
Brackets(int low, int high) {
this.low = low;
this.high = high;
}
public int getLow() {
return low;
}
public int getHigh() {
return high;
}
public boolean isWithin(int value) {
return value >= low && value <= high;
}
public String toString() {
return "Bracket " + low + " to " + high;
}
}
private Bracket bracket;
private String name;
public Person(String name, Bracket bracket) {
this.bracket = bracket;
this.name = name;
}
public String toString() {
return name + " in " + bracket;
}
}
Saw a post regarding this for Java a while back, check out http://www.javaspecialists.eu/archive/Issue161.html .
I would like to be able to add values to C# enumerations which are combinations of existing values. For example (this is what I want to do):
AnchorStyles is defined as
public enum AnchorStyles {
None = 0,
Top = 1,
Bottom = 2,
Left = 4,
Right = 8,
}
and I would like to add an AnchorStyles.BottomRight = Right + Bottom so instead of saying
my_ctrl.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
I can just say
my_ctrl.Anchor = AnchorStyles.BottomRight;
This doesn't cause any of the problems that have been mentioned above, so it would be nice if it was possible.
A temporary/local workaround, when you just want very local/one time usage:
enum Animals { Dog, Cat }
enum AnimalsExt { Dog = Animals.Dog, Cat= Animals.Cat, MyOther}
// BUT CAST THEM when using:
var xyz = AnimalsExt.Cat;
MethodThatNeedsAnimal( (Animals)xyz );
See all answers at: Enum "Inheritance"
You can't inherit from/extend an enum, you can use attributes to declare a description. If you're looking for an integer value, that's built-in.
Hmmm - as far as I know, this can't be done - enumerations are written at design-time and are used as a convenience to the programmer.
I'm pretty sure that when the code is compiled, the equivalent values will be substituted for the names in your enumeration, thereby removing the concept of an enumeration and (therefore) the ability to extend it.
Some time back even i wanted to do something like this and found that enum extensions would voilate lot of basic concepts... (Not just polymorphisim)
But still u might need to do if the enum is declared in external library and
Remember you should make a special caution when using this enum extensions...
public enum MyEnum { A = 1, B = 2, C = 4 }
public const MyEnum D = (MyEnum)(8);
public const MyEnum E = (MyEnum)(16);
func1{
MyEnum EnumValue = D;
switch (EnumValue){
case D: break;
case E: break;
case MyEnum.A: break;
case MyEnum.B: break;
}
}
As far as java is concerned it is not allowed because adding elements to an enum would effectively create a super class rather than a sub class.
Consider:
enum Person (JOHN SAM}
enum Student extends Person {HARVEY ROSS}
A general use case of Polymorphism would be
Person person = Student.ROSS; //not legal
which is clearly wrong.

Categories