views:

394

answers:

4

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.

+3  A: 

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 Skeet
A: 

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.

Victor
Err, of course it does? You're assigning tomorrow to the value of today THEN incrementing it.
Garry Shutler
You wouldn't expect tomorrow to be 2 after: `int today = 1; int tomorrow = today++;`
Garry Shutler
ahh nuts you're right! thanks Garry
Victor
I'd have to agree that your `Next` method really shouldn't accept a date parameter -- it should just give the next date after the associated instance. Either that or you could make it a static method.
Dan Tao
+4  A: 

System.DateTime.AddDays

Save yourself an epic, date-based headache.

Garry Shutler
A: 

DateTime SchDate= DateTime.Now; SchDate= SchDate.AddDays(1);

what ever may be the day or month/yr you can add

sajith.O.B