Page 1 of 1

Object Equality

Posted: Sat Mar 02, 2019 2:36 am
by jyoung
If I define an object as such

Code: Select all

function options(*DIRECT)
begin_com role(*EXTENDS #PRIM_OBJT *LISTFIELDS #ListFields)

group_by name(#ListFields) fields(#HVBTC #HVPLCN #HVPLD #wk_Bool1)

define_pty name(TypeCode) get(*AUTO #HVBTC) set(*AUTO #HVBTC)
define_pty name(ControlNumber) get(*AUTO #HVPLCN) set(*AUTO #HVPLCN)
define_pty name(Description) get(*AUTO #HVPLD) set(*AUTO #HVPLD)
define_pty name(HasOptedIn) get(*AUTO #wk_Bool1) set(*AUTO #wk_Bool1)

end_com
How do you handle object equality?

Basically I want to store an instance of this object and then later compare that instance with another instance and see if they are the same.
such as

Code: Select all

if (#MyFirstObject = #MySecondObject)
* the two are the same
endif
In .NET, I could override Equals and GetHashCode or implement a Comparer. Is there something similar in LANSA or am I stuck doing property comparison?
Such as

Code: Select all

if (#CurrentPlan *IsNot *NULL)
if ((#CurrentPlan.TypeCode = #lItem.TypeCode) *AndIf (#CurrentPlan.ControlNumber = #lItem.ControlNumber))
* they are the same	
endif

endif

Re: Object Equality

Posted: Sat Mar 02, 2019 4:34 am
by Pablo
Hi Joe,

I assume you have a collection and some component that at one point you set to refer to the one you want to store as the current one:

Define_Com Class(#Prim_lcol<#MyListFieldsComponent>) Name(#ListEntries)
Define_Com Class(#MyListFieldsComponent) Name(#CurrentEntry) Reference(*DYNAMIC)

Then:

For Each(#Entry) In(#ListEntries)

If (#Entry *IsEqualTo #CurrentEntry)
* Your Logic Here
Endif

Endfor

Regards,
Pablo

Re: Object Equality

Posted: Sat Mar 02, 2019 5:15 am
by jyoung
That is what I was looking for.

Thanks!