C# Operator Overloading post-fix increment
I'm coding a date class and am having trouble with the post-fix increment (the prefix increment seems fine).
Here is the sample code:
public class date
{
int year,
month,
day;
public date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
static public date operator ++(date d开发者_开发技巧)
{
return d.Next(d);
}
}
The method "Next(date d)" takes a date and returns tomorrows date (I left it out for brevity). I'm to young in C# to understand why the prefix is fine but postfix increment does nothing. But remember in C++ we would have to have two methods instead of just one - for prefix and postfix increments.
Also no errors or warnings on compile.
System.DateTime.AddDays
Save yourself an epic, date-based headache.
Well you haven't shown the Next
method, which would be kinda handy... in particular showing why it needs to take a date
as an argument. My guess is that your Next
method is flawed.
You also haven't shown an example of it failing for postincrement. Here's a simplified example which shows that it does work:
using System;
public class Date
{
int year, month, day;
public Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
public static Date operator ++(Date d)
{
return d.Next();
}
private Date Next()
{
// Just a toy implementation, obviously
return new Date(day + 1, month, year);
}
static void Main()
{
Date x = new Date(1, 2, 3);
x++;
Console.WriteLine(x.day); // Prints 2
}
}
Note how it prints 2, showing that the day has been incremented (or rather, x
now refers to a new instance of Date
which has an incremented day value).
Personally I don't think I'd introduce a ++ operator for a Date
class anyway, but never mind. I'd also suggest that the constructor should be year/month/day rather than day/month/year; that's more conventional, and fits in better with situations where you want to allow more precision with more parameters.
Jon thanks, you're absolutely right let me attach the missing next() method that is inside the class:
public date Next(date d)
{
if (!d.valid()) return new date();
date ndat = new date((d.Day() + 1), d.Month(), d.Year());
if (ndat.valid()) return ndat;
ndat = new date(1, (d.Month() + 1), d.Year());
if (ndat.valid()) return ndat;
ndat = new date(1, 1, (d.Year() + 1));
return ndat;
}
Since this uses valid() I'll attach this also:
public bool valid()
{
// This method will check the given date is valid or not.
// If the date is not valid then it will return the value false.
if (year < 0) return false;
if (month > 12 || month < 1) return false;
if (day > 31 || day < 1) return false;
if ((day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)))
return false;
if (day == 30 && month == 2) return false;
if (day == 29 && month == 2 && (year % 4) != 0) return false;
if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false;
/* ASIDE. The duration of a solar year is slightly less than 365.25 days. Therefore,
years that are evenly divisible by 100 are NOT leap years, unless they are also
evenly divisible by 400, in which case they are leap years. */
return true;
}
the Day(), Month(), and Year() I think are self explanatory but let me know if they're needed. I also have a previous() method that does the opposite of next() which I want to use in the -- decrement method.
Now in my program, I have
class Program
{
static void Main()
{
date today = new date(7,10,1985);
date tomoz = new date();
tomorrow = today++;
tomorrow.Print(); // prints "7/10/1985" i.e. doesn't increment
Console.Read();
}
}
So it doesn't actually fail it just prints todays date instead of tomorrow's but works correctly if I had used ++today instead.
Regarding the order D/M/Y, yep I agree, with higher frequency data I can see how that improves things, I'll move on to fixing that next.
DateTime SchDate= DateTime.Now;
SchDate= SchDate.AddDays(1);
what ever may be the day or month/yr you can add
I have an additional comment that is probably too late for the original poster, but may be useful to anyone reading in the future.
Looking at your implementation of "valid":
if (day == 29 && month == 2 && (year % 4) != 0) return false;
if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false;
It will fail on 29/2/2100, which is a valid date but your method says it's not.
精彩评论