fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: implement compare to own classes

implement compare to own classes 3 months 2 weeks ago #10063

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1853
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
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10064

  lumberjack's Avatar lumberjack Offline Posts: 610
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 >, <, !=?

PUBLIC [STATIC] OPERATOR +(var1 AS <Type>, var2 AS <Type>) AS <ReturnType>
RETURN <Method/Function Name>(<var1>, <var2>)
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10065

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1853
Hi Johan,

thanks, it works now.

This is my class:
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
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

implement compare to own classes 3 months 2 weeks ago #10066

  lumberjack's Avatar lumberjack Offline Posts: 610
Hi Wolfgang,

wriedmann wrote: thanks, it works now.

class XbDate
	protect _oDatum as Nullable<DateTime>
public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic

Glad you got it working.
@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.:
IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10067

  FFF's Avatar FFF Offline Posts: 738

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.:

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd

Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.
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

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10068

  lumberjack's Avatar lumberjack Offline Posts: 610
Hi Karl,

FFF wrote:

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.:

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd

Not sure i'm with you re "new" operators, at least, when there meaning is not intuitively understandable.
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...)

Well it was due to PG's PostGIS operators that I asked the question. See this PostGISOperators
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10069

  Chris's Avatar Chris Away Posts: 1460

lumberjack wrote: Hi Wolfgang,

wriedmann wrote: thanks, it works now.

class XbDate
	protect _oDatum as Nullable<DateTime>
public static operator >( oDate1 as XbDate, oDate2 as XbDate ) as logic

Glad you got it working.
@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?


Operator methods are always STATIC, so if you do not explicitly include it, the X# compiler always adds it itself anyway.

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.:

IF oDate <=> dtStart, dtEnd // oDate >= dtStart .AND. oDate <= dtEnd


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.

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".
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 2 weeks ago #10071

  lumberjack's Avatar lumberjack Offline Posts: 610
Hi Chris,
Thanks for your answer.

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".

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.

Regards,
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 1 week ago #10092

  robert's Avatar robert Offline Posts: 1310
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:
github.com/X-Sharp/XSharpPublic/blob/fea...rp.RT/Types/Date.prg

Robert
XSharp Development Team
The Netherlands

Please Log in or Create an account to join the conversation.

implement compare to own classes 3 months 1 week ago #10094

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1853
Hi Robert,

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

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1