Very Urgent Please -- Overloading ++ operator doesn't function as intended.
-
Hi, I'm overloading the ++ operator for my Day Class. I have the implementation for this overload within my class and when I tested this in the debug mode, my Day which is supposed to increment as I've coded doesn't increse. It basically gives out an object of type Day itself with the next date. Here is my code. Any help with this would be appreciated. Please do help me out with this.
public static Day operator ++(Day d) { Day nxtDay; int nxtDate; //GregorianCalendar gc = new GregorianCalendar(); try { nxtDate = d._dayofmonth + 1; nxtDate += 1 ; nxtDay = new Day(new DateTime(d.Year, d.Month, nxtDate)); } catch (Exception e) { e.GetType(); int nxtDayMonth = (d.Month == 12) ? 1 : d.Month + 1; int nxtDayYear = (nxtDayMonth == 1)? d.Year + 1: d.Year; nxtDay = new Day(gc.ToDateTime(nxtDayYear,nxtDayMonth,1,1,1,1,1)); } return nxtDay; }
Thanks ~~~~~~~~~~~~~~~~~~~~~~~~ Shravan Addaypally .NET Apps Developer BellSouth Billing Inc. Birmingham, AL 35205 ~~~~~~~~~~~~~~~~~~~~~~~~ -
Hi, I'm overloading the ++ operator for my Day Class. I have the implementation for this overload within my class and when I tested this in the debug mode, my Day which is supposed to increment as I've coded doesn't increse. It basically gives out an object of type Day itself with the next date. Here is my code. Any help with this would be appreciated. Please do help me out with this.
public static Day operator ++(Day d) { Day nxtDay; int nxtDate; //GregorianCalendar gc = new GregorianCalendar(); try { nxtDate = d._dayofmonth + 1; nxtDate += 1 ; nxtDay = new Day(new DateTime(d.Year, d.Month, nxtDate)); } catch (Exception e) { e.GetType(); int nxtDayMonth = (d.Month == 12) ? 1 : d.Month + 1; int nxtDayYear = (nxtDayMonth == 1)? d.Year + 1: d.Year; nxtDay = new Day(gc.ToDateTime(nxtDayYear,nxtDayMonth,1,1,1,1,1)); } return nxtDay; }
Thanks ~~~~~~~~~~~~~~~~~~~~~~~~ Shravan Addaypally .NET Apps Developer BellSouth Billing Inc. Birmingham, AL 35205 ~~~~~~~~~~~~~~~~~~~~~~~~This might seem like a dumb question, but why not use the already built-in DateTime object? Check this example out from MSDN:
System.DateTime dTime = new System.DateTime(1980, 8, 5); // tSpan is 17 days, 4 hours, 2 minutes and 1 second. System.TimeSpan tSpan = new System.TimeSpan(17, 4, 2, 1); // Result gets 8/22/1980 4:02:01 AM. // You can also use a DateTime object with the '+' operator System.DateTime result = dTime + tSpan;
It seems to me to get the same job done without all the hassle of making your own object. ~javier lozano (blog || email) -
Hi, I'm overloading the ++ operator for my Day Class. I have the implementation for this overload within my class and when I tested this in the debug mode, my Day which is supposed to increment as I've coded doesn't increse. It basically gives out an object of type Day itself with the next date. Here is my code. Any help with this would be appreciated. Please do help me out with this.
public static Day operator ++(Day d) { Day nxtDay; int nxtDate; //GregorianCalendar gc = new GregorianCalendar(); try { nxtDate = d._dayofmonth + 1; nxtDate += 1 ; nxtDay = new Day(new DateTime(d.Year, d.Month, nxtDate)); } catch (Exception e) { e.GetType(); int nxtDayMonth = (d.Month == 12) ? 1 : d.Month + 1; int nxtDayYear = (nxtDayMonth == 1)? d.Year + 1: d.Year; nxtDay = new Day(gc.ToDateTime(nxtDayYear,nxtDayMonth,1,1,1,1,1)); } return nxtDay; }
Thanks ~~~~~~~~~~~~~~~~~~~~~~~~ Shravan Addaypally .NET Apps Developer BellSouth Billing Inc. Birmingham, AL 35205 ~~~~~~~~~~~~~~~~~~~~~~~~As Javier said, just use the
DateTime
structure. A few things, though: why are you just callinge.GetType
without actually using it? If you don't plan on using the exception for information (and I'm betting you are just callinge.GetType
to get rid of the warning about declaring a type that you don't use), then don't declare it. Simply usingcatch
orcatch (Exception)
is suitable (both of those result in the same behavior). Finally, is this a class or a struct you're using? A struct is a value type, so any changes you make to it happen on a copy - not the original object. A reference type (a class) will always be the same object, though (refers to the same object, rather). Depending on how you use yourDay
struct (assuming it's a struct), this could be the problem why its value appears unchanged.Microsoft MVP, Visual C# My Articles