Results 1 to 8 of 8

Thread: Operator overloading Issue

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Operator overloading Issue

    can anyone tell me ?why it is printing 76 instead of 86. return new Time(first.hours+second.hours, first.minutes + second.minutes);
    here is the following code .what i have written.kindly let me know please...
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace oExample8
    {
        public struct Time
        {
            public Time(int hours, int minutes)
            {
                this.hours = hours;
                this.minutes = minutes;
            }
            int hours, minutes;
            public static Time operator +(Time first, Time second)
            {
                return new Time(first.hours+second.hours, first.minutes + second.minutes);
    
            }
            public static void Main()
            {
                Time start = new Time();
                Time duration = new Time();
                Time finish = new Time();
                start.hours = 12;
                duration.minutes = 10;
                duration.hours = 1;
                duration.minutes = 76;
                finish = start + duration;
                Console.WriteLine("Finish time would be :{0} hours and {1} minutes.", finish.hours, finish.minutes);
                Console.Read();
            }
    
        }
    }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Operator overloading Issue

    Thats because you set duration.minutes to 76. It doesn't matter that you set it to 10 earlier, because you are overwriting that values with 76 two lines down.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Operator overloading Issue

    Thats because you set duration.minutes to 76. It doesn't matter that you set it to 10 earlier, because you are overwriting that values with 76 two lines down.
    but in the case of hours it is printing properly 12+1.if possible kindly demostrate any small example regarding operator overloading.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Operator overloading Issue

    I think this is a simple case of you just not reading your own code properly. Look at this form your own code:
    Code:
                start.hours = 12;
                duration.minutes = 10;
                duration.hours = 1;
                duration.minutes = 76;
    Presumably that should be:
    Code:
                start.hours = 12;
                start.minutes = 10;
                duration.hours = 1;
                duration.minutes = 76;
    We can all overlook the obvious at times but in this case you obviously didn't bother to do any debugging either, or you'd have seen that start.minutes was zero.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Operator overloading Issue

    Thank You.
    Last edited by firoz.raj; Jul 19th, 2010 at 11:21 PM.

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Operator overloading Issue

    >>, <<, +, -, /, *, &#37;, ==, !=.

    And you could have found that in 3 seconds using Google.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Operator overloading Issue

    Execuse me .Can you tell me ?why the following code is Not Compiling?.let me know some hints please.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Example13
    {
        class Boxme
        {
            int itakeObj(Object x)
            {
                Console.WriteLine(x);
            }
            object iSpitObs()
            {
          
           int v;
           v=5;
           BoxMe Up=new Boxme();
           up.itakeObjs(virtual);
           int boxBox=(int)up.isitObjs();
                }
        }
    }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Operator overloading Issue

    Quote Originally Posted by firoz.raj View Post
    Execuse me .Can you tell me ?why the following code is Not Compiling?.let me know some hints please.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Example13
    {
        class Boxme
        {
            int itakeObj(Object x)
            {
                Console.WriteLine(x);
            }
            object iSpitObs()
            {
          
           int v;
           v=5;
           BoxMe Up=new Boxme();
           up.itakeObjs(virtual);
           int boxBox=(int)up.isitObjs();
                }
        }
    }
    This question seems unrelated to the topic of this thread so it should have been posted in a new thread. Please keep each thread to a single topic and each topic to a single thread.

    You have no variable named 'virtual' so you can't pass a variable named 'virtual' to a method.

    You have a function whose type is not void that doesn't execute a 'return' statement no matter which path execution takes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width