TheSwamp
Code Red => .NET => Topic started by: BillZndl 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.
public static bool operator <=(DateTime t1, DateTime t2);
In my method I have:
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?
-
Hi,
Just use : t1 <= t2
You method must have a return type i.e. boolean:
public bool YourMethod(string NewStrLine)
{
DateTime t1 = Convert.ToDateTime(NewStrLine);
DateTime t2 = DateTime.Today;
return = t1 <= t2;
}
-
For those of you not familiar with operator overloding,
you can overload operators for custom classes and structures.
Here is a example overloading the + operator
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
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
public static bool operator <=(DateTime t1, DateTime t2)
{
return &t1.InternalTicks <= &t2.InternalTicks;
}
-
Sheesh, so simple. :-o
Glad I asked.
Thanks!
I'll have to look at what Jeff posted at a later time but thanks for that too. ^-^
-
For those of you not familiar with operator overloding,
you can overload operators for custom classes and structures.
Here is a example overloading the + operator
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! :wink:
-
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 >.
So you must add
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);
}
The whole thing
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);
}
}