### Author Topic: Use of an operator function inside of a method.  (Read 1914 times)

0 Members and 1 Guest are viewing this topic.

#### BillZndl

• Guest
##### Use of an operator function inside of a method.
« on: August 25, 2011, 08:57:27 AM »

This is a funcion I got of the microsoft site that returns true or false depending on which date is larger numerically.

Code: [Select]
`public static bool operator <=(DateTime t1, DateTime t2);`

In my method I have:

Code: [Select]
`public MyMethod (string NewStrLine){       DateTime t1 = Convert.ToDateTime(NewStrLine);       DateTime t2 = DateTime.Today;       bool b1 = ???}`
How do I incorporate or use the function inside of  my method?

#### gile

• Gator
• Posts: 2507
• Marseille, France
##### Re: Use of an operator function inside of a method.
« Reply #1 on: August 25, 2011, 09:12:01 AM »
Hi,

Just use : t1 <= t2

You method must have a return type i.e. boolean:
Code: [Select]
`        public bool YourMethod(string NewStrLine)        {            DateTime t1 = Convert.ToDateTime(NewStrLine);            DateTime t2 = DateTime.Today;            return = t1 <= t2;        }`
Speaking English as a French Frog

#### Jeff H

• Needs a day job
• Posts: 6150
##### Re: Use of an operator function inside of a method.
« Reply #2 on: August 25, 2011, 10:04:18 AM »
For those of you not familiar with operator overloding,
you can overload operators for custom classes and structures.

Code: [Select]
`      public class Point    {        public double X { get; set; }        public double Y { get; set; }        public Point(double x, double y)        {            X = x;            Y = y;        }        public static Point operator +(Point p1, Point p2)        {            return new Point(p1.X + p2.X, p1.Y + p2.Y);        }    }`
You could implement IComparable and do something like this for Comparison operators

Code: [Select]
`    public class Point: IComparable    {        public double X { get; set; }        public double Y { get; set; }        public Point(double x, double y)        {            X = x;            Y = y;        }        public static Point operator +(Point p1, Point p2)        {            return new Point(p1.X + p2.X, p1.Y + p2.Y);        }         public int CompareTo(object obj)        {            Point pnt = (Point)obj;            if (this.X > pnt.X && this.Y > pnt.Y)            {                return 1;            }            else if (this.X < pnt.X && this.Y < pnt.Y)            {                return -1;            }            else            {                return 0;            }        }        public static bool operator <=(Point p1, Point p2)        {            return (p1.CompareTo(p2) <= 0);        }        public static bool operator <(Point p1, Point p2)        {            return (p1.CompareTo(p2) < 0);        }    }`
And here is the function that Bill posted decompiled From System.DateTime

Code: [Select]
` public static bool operator <=(DateTime t1, DateTime t2){ return &t1.InternalTicks <= &t2.InternalTicks;}`
« Last Edit: August 25, 2011, 10:24:23 AM by Jeff H »

#### BillZndl

• Guest
##### Re: Use of an operator function inside of a method.
« Reply #3 on: August 25, 2011, 10:15:09 AM »
Sheesh, so simple.
Thanks!

I'll have to look at what Jeff posted at a later time but thanks for that too.

#### BillZndl

• Guest
##### Re: Use of an operator function inside of a method.
« Reply #4 on: August 25, 2011, 10:57:18 AM »
For those of you not familiar with operator overloding,
you can overload operators for custom classes and structures.

Code: [Select]
`      public class Point    {        public double X { get; set; }        public double Y { get; set; }        public Point(double x, double y)        {            X = x;            Y = y;        }        public static Point operator +(Point p1, Point p2)        {            return new Point(p1.X + p2.X, p1.Y + p2.Y);        }    }`

I did a little testing.
Pretty neat stuff there Jeff.
Thanks again!

#### Jeff H

• Needs a day job
• Posts: 6150
##### Re: Use of an operator function inside of a method.
« Reply #5 on: August 25, 2011, 01:58:51 PM »
For completeness sake.

I just tried to build the solution with the second example for other code in it to use, but
it will fail because if you overload <= you have to overload >= and vise versa. The same goes for < and >.

`     public static bool operator >=(Point p1, Point p2)        {            return (p1.CompareTo(p2) >= 0);        }        public static bool operator >(Point p1, Point p2)        {            return (p1.CompareTo(p2) > 0);        }`
`           public class Point: IComparable    {        public double X { get; set; }        public double Y { get; set; }        public Point(double x, double y)        {            X = x;            Y = y;        }        public static Point operator +(Point p1, Point p2)        {            return new Point(p1.X + p2.X, p1.Y + p2.Y);        }         public int CompareTo(object obj)        {            Point pnt = (Point)obj;            if (this.X > pnt.X && this.Y > pnt.Y)            {                return 1;            }            else if (this.X < pnt.X && this.Y < pnt.Y)            {                return -1;            }            else            {                return 0;            }        }        public static bool operator <=(Point p1, Point p2)        {            return (p1.CompareTo(p2) <= 0);        }        public static bool operator <(Point p1, Point p2)        {            return (p1.CompareTo(p2) < 0);        }        public static bool operator >=(Point p1, Point p2)        {            return (p1.CompareTo(p2) >= 0);        }        public static bool operator >(Point p1, Point p2)        {            return (p1.CompareTo(p2) > 0);        }    }`