How to operator overload negative sign to subtract custom time objects

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2048 Views - Last Post: 21 March 2011 - 11:11 AM Rate Topic: -----

#1 uglycode  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 19
  • Joined: 17-February 11

How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 11:39 AM

Can't figure out how to create operator overload "-" for the Time class.

This post has been edited by uglycode: 20 March 2011 - 11:59 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How to operator overload negative sign to subtract custom time objects

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 11:46 AM

Why are you re-inventing the wheel?
The System.DateTime already has numerous methods for adding and subtracting time to get spans and so on.

This post has been edited by tlhIn`toq: 20 March 2011 - 11:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 uglycode  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 19
  • Joined: 17-February 11

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 11:49 AM

Will it allow me to find out how much time it will take to get from point A to point B.
You should be able to add, subtract, divide, compare values.

This post has been edited by uglycode: 20 March 2011 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 11:53 AM

See TimeSpan Structure
Was This Post Helpful? 2
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 12:00 PM

Operator Overloads
Sorry but couldn't locate a C# one.

This post has been edited by AdamSpeight2008: 20 March 2011 - 12:01 PM

Was This Post Helpful? 1
  • +
  • -

#6 uglycode  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 19
  • Joined: 17-February 11

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 12:00 PM

Interesting!
Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 12:02 PM

View Postuglycode, on 20 March 2011 - 12:49 PM, said:

Will it allow me to find out how much time it will take to get from point A to point B.
You should be able to add, subtract, divide, compare values.


And again I say you are re-inventing the wheel. This already exists in the DatTime object.
http://www.google.co...=UTF-8&oe=UTF-8

DateTime1 - DateTim2 = new timespan.

I now suspect this is a school project and we can't give you code for your homework.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 12:06 PM

Does it matter if uglycode is replicating some existing functionality? When they want learn how to create an operator?
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 01:09 PM

Operator Overloading Tutorial (C#)
Was This Post Helpful? 1
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 01:54 PM

View PostAdamSpeight2008, on 20 March 2011 - 01:06 PM, said:

Does it matter if uglycode is replicating some existing functionality? When they want learn how to create an operator?


The OP has demonstrated an understanding of how to create an operator

Quote

public static Time operator +(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.h = obj1.h + obj2.h;
                
                temp.m = obj1.m + obj2.m;
                while (temp.m > 59)
                {
                    ++temp.h;
                    temp.m -= 60;
                }

                temp.s = obj1.s + obj2.s;
                while (temp.s > 59)
                {
                    ++temp.m;
                    temp.s -= 60;
                }
                return temp;               
            }



He was just lost on the logic for subtracting rather than adding.

So I *asked* why he was re-inventing the wheel. If it was for an educational exercise then great. If it was because he didn't know there was a pre-existing way that could save him a LOT of time then pointing out the DateTime object could be just as beneficial.

I was just trying to clarify if the true goal was to learn to create an operator, or to be able to get a timespan from two different time-like objects.
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 01:58 PM

I have to agree with tlhInq here. It's our job to let people know if something better already exists, unless the OP explicitly asks for a way to reinvent the wheel.

Since the OP just asked how to do it, showing him an already existing class was a good call.
Was This Post Helpful? 1
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 20 March 2011 - 02:00 PM

Since the OP completely changed his original question right after I voiced a concern this might be an attempt to get someone else to supply code for his homework; I am now further convinced that is the true goal of the post.

Original post:
Spoiler

Was This Post Helpful? 1
  • +
  • -

#13 uglycode  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 19
  • Joined: 17-February 11

Re: How to operator overload negative sign to subtract custom time objects

Posted 21 March 2011 - 10:55 AM

View PosttlhIn`toq, on 20 March 2011 - 09:00 PM, said:

Since the OP completely changed his original question right after I voiced a concern this might be an attempt to get someone else to supply code for his homework; I am now further convinced that is the true goal of the post.


Please remove my original code from your post. The whole point is so that people don't copy off me.

Yes, it is a project.

No, I don't want YOUR code. I just needed some guidance. You all pointed to DateTime and TimeSpan structures and it helped alot.

Yes, I'm learning how to do operator overloading and reinventing of the wheel is uneccessary for my project.

This post has been edited by uglycode: 21 March 2011 - 11:03 AM

Was This Post Helpful? -1
  • +
  • -

#14 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to operator overload negative sign to subtract custom time objects

Posted 21 March 2011 - 11:04 AM

That's not how this forum works. All code should be free for everyone to learn from. We frown upon this 'my code' mentality here.
Was This Post Helpful? 2
  • +
  • -

#15 uglycode  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 19
  • Joined: 17-February 11

Re: How to operator overload negative sign to subtract custom time objects

Posted 21 March 2011 - 11:05 AM

View PostSergio Tapia, on 21 March 2011 - 06:04 PM, said:

That's not how this forum works. All code should be free for everyone to learn from. We frown upon this 'my code' mentality here.


Okay.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2