Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Very Urgent Please -- Overloading ++ operator doesn't function as intended.

Very Urgent Please -- Overloading ++ operator doesn't function as intended.

Scheduled Pinned Locked Moved C#
csharpdebugginghelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1124279
    wrote on last edited by
    #1

    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 ~~~~~~~~~~~~~~~~~~~~~~~~

    J H 2 Replies Last reply
    0
    • U User 1124279

      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 ~~~~~~~~~~~~~~~~~~~~~~~~

      J Offline
      J Offline
      Javier Lozano
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • U User 1124279

        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 ~~~~~~~~~~~~~~~~~~~~~~~~

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        As Javier said, just use the DateTime structure. A few things, though: why are you just calling e.GetType without actually using it? If you don't plan on using the exception for information (and I'm betting you are just calling e.GetType to get rid of the warning about declaring a type that you don't use), then don't declare it. Simply using catch or catch (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 your Day struct (assuming it's a struct), this could be the problem why its value appears unchanged.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups