implement compare to own classes
implement compare to own classes
Hi Chris,
I have implemented my own date class to be used in applications written in Core dialect.
How can I implement the compare operators >, <, !=?
Wolfgang
I have implemented my own date class to be used in applications written in Core dialect.
How can I implement the compare operators >, <, !=?
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
implement compare to own classes
Hi Wolfgang,
wriedmann wrote: I have implemented my own date class to be used in applications written in Core dialect.
How can I implement the compare operators >, <, !=?
Code: Select all
PUBLIC [STATIC] OPERATOR +(var1 AS <Type>, var2 AS <Type>) AS <ReturnType>
RETURN <Method/Function Name>(<var1>, <var2>)
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
implement compare to own classes
Hi Johan,
thanks, it works now.
This is my class:
Wolfgang
thanks, it works now.
This is my class:
Code: Select all
class XbDate
protect _oDatum as Nullable<DateTime>
constructor()
_oDatum := null
return
constructor( oDatum as Nullable<DateTime> )
_oDatum := oDatum
return
constructor( nYear as int, nMonth as int, nDay as int )
_oDatum := null
try
_oDatum := DateTime{ nYear, nMonth, nDay }
end try
return
constructor( cDateString as string )
local nYear as int
local nMonth as int
local nDay as int
_oDatum := null
try
nYear := 0
nMonth := 0
nDay:= 0
if Int32.TryParse( cDateString:Substring( 0, 4 ), ref nYear ) .and. ;
Int32.TryParse( cDateString:Substring( 4, 2 ), ref nMonth ) .and. ;
Int32.TryParse( cDateString:Substring( 6, 2 ), ref nDay )
_oDatum := DateTime{ nYear, nMonth, nDay }
endif
end try
return
property DateTime as Nullable<DateTime> get _oDatum
property DateString as string
get
if _oDatum == null
return ""
endif
return ( ( DateTime ) _oDatum ):ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture )
end get
end property
property Year as int
get
if _oDatum == null
return 0
endif
return ( ( DateTime ) _oDatum ):Year
end get
end property
property Month as int
get
if _oDatum == null
return 0
endif
return ( ( DateTime ) _oDatum ):Month
end get
end property
property Day as int
get
if _oDatum == null
return 0
endif
return ( ( DateTime ) _oDatum ):Day
end get
end property
public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime > oDate2:DateTime
public static operator <( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime > oDate2:DateTime
public static operator <=( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime <= oDate2:DateTime
public static operator >=( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime >= oDate2:DateTime
public static operator !=( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime != oDate2:DateTime
public static operator ==( oDate1 as XbDate, oDate2 as XbDate ) as logic
return oDate1:DateTime == oDate2:DateTime
method GetHashCode() as int
return _oDatum:GetHashCode()
method Equals( oDate as object ) as logic
local lReturn as logic
if oDate is XbDate
lReturn := ( ( ( XbDate ) oDate ):DateTime == self:DateTime )
else
lReturn := false
endif
return lReturn
end class
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
implement compare to own classes
Hi Wolfgang,
@DevTeam: Maybe one of you guys can enlighten us when to use "STATIC" and in which cases one would not declare it a static operator?
Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:
Glad you got it working.wriedmann wrote:thanks, it works now.Code: Select all
class XbDate protect _oDatum as Nullable<DateTime> public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
@DevTeam: Maybe one of you guys can enlighten us when to use "STATIC" and in which cases one would not declare it a static operator?
Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:
Code: Select all
IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
implement compare to own classes
Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.lumberjack wrote:Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:Code: Select all
IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
So, if "<=>" means both sides inclusive, i'd expect exclusive to be ">=<", and naturally the "<=<" and ">=>" variants
(Interisting, years ago i ventured a shy suggestion in PG, which uses similar schemes using [], for a [=[ instead of [=) (IIRC...) - got shot down...)
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
implement compare to own classes
Hi Karl,
Well it was due to PG's PostGIS operators that I asked the question. See this PostGISOperatorsFFF wrote:Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.lumberjack wrote:Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:Code: Select all
IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
So, if "<=>" means both sides inclusive, i'd expect exclusive to be ">=<", and naturally the "<=<" and ">=>" variants
(Interisting, years ago i ventured a shy suggestion in PG, which uses similar schemes using [], for a [=[ instead of [=) (IIRC...) - got shot down...)
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
implement compare to own classes
Operator methods are always STATIC, so if you do not explicitly include it, the X# compiler always adds it itself anyway.lumberjack wrote:Hi Wolfgang,Glad you got it working.wriedmann wrote:thanks, it works now.Code: Select all
class XbDate protect _oDatum as Nullable<DateTime> public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic
@DevTeam: Maybe one of you guys can enlighten us when to use "STATIC" and in which cases one would not declare it a static operator?
That cannot be done at the user level, because the set of the operators that can be redefined in code is standard. But anything can be implemented at the compiler level, like for example the different = and == operators, which is unique in X# (and xBase languages in general), but in .Net there's only one equality operator.lumberjack wrote: Another more theoretical question:
Is there a way to have an operator, lets say between dates "<=>" which is inclusive of start and end date, or exclusive "<->" e.g.:Code: Select all
IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
I think it would be nice having a "between" operator, will add this as an enhancement request, but hope you understand it will not take high priority tight now. Btw, maybe it would be possible to use the already existing $ operator (which is also xBase specific and cannot be overloaded) to cover also "in between" in addition to "inside string".
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
- lumberjack
- Posts: 727
- Joined: Fri Sep 25, 2015 3:11 pm
- Location: South Africa
implement compare to own classes
Hi Chris,
Thanks for your answer.
Regards,
Thanks for your answer.
No worries, was just wondering if one can define your own "special" operators, as is possible in PostgreSQL. No worries to implement if not part of .NET.Chris wrote: I think it would be nice having a "between" operator, will add this as an enhancement request, but hope you understand it will not take high priority tight now. Btw, maybe it would be possible to use the already existing $ operator (which is also xBase specific and cannot be overloaded) to cover also "in between" in addition to "inside string".
Regards,
______________________
Johan Nel
Boshof, South Africa
Johan Nel
Boshof, South Africa
implement compare to own classes
Wolfgang,
The best place to look for the syntax of this kind of things is the source of the X# runtime on Github.
For example the source to the date type is in:
https://github.com/X-Sharp/XSharpPublic ... s/Date.prg
Robert
The best place to look for the syntax of this kind of things is the source of the X# runtime on Github.
For example the source to the date type is in:
https://github.com/X-Sharp/XSharpPublic ... s/Date.prg
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
The Netherlands
robert@xsharp.eu
implement compare to own classes
Hi Robert,
thank you very much!
Sometimes I have problems to find the right place on GitHub where to look.
Wolfgang
thank you very much!
Sometimes I have problems to find the right place on GitHub where to look.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it