Is it possible to invert an object or mark it as negative?
This is what I want to achieve:
DateTime.Now.Add(-TimeUnits.Week)
This should return the current date time minus 7 days.
Therefore, I need to detect in the method Addif the passed object is negative or not!
I looked at Struct, but my experience with them is too little to say whether this is possible or not! I currently have an enumeration and, of course, it is very limited. I am not attached to an enumeration, so it could be any other object!
My listing TimeUnits:
public enum TimeUnits
{
Once = 0,
Day = 1,
Week = 2,
Month = 3,
Quarter = 4,
Year = 5
}
I have an extension method on DateTimewhere I want to pass an object to a method Addas follows:
private static DateTime? Add(this DateTime current, TimeUnits unitOfTime)
{
switch (unitOfTime)
{
case TimeUnits.Once:
return null;
case TimeUnits.Day:
return current.AddDays(1);
case TimeUnits.Week:
return current.AddDays(7);
case TimeUnits.Month:
return current.AddMonths(1);
case TimeUnits.Quarter:
return current.AddMonths(3);
case TimeUnits.Year:
return current.AddYears(1);
default:
throw new ArgumentOutOfRangeException("unitOfTime");
}
}