xsharp.eu • Assignment when a value is of a specific class
Page 1 of 1

Assignment when a value is of a specific class

Posted: Mon May 19, 2025 10:45 am
by wriedmann
Hi Chris,
if I remember correctly, there is an operator or expression that lets me assign a value to a variable when it is of a specific type.
Normally I write:

Code: Select all

if oValue is MyClass
  oClass := oValue
endif
But I was not able to find anything in the help....
Wolfgang

Re: Assignment when a value is of a specific class

Posted: Mon May 19, 2025 1:15 pm
by Chris
Hi Wolfgang,

If you have (in the Core dialect) :

LOCAL oValue AS OBJECT
LOCAL oClass AS MyClass

then you can do:

oClass := (MyClass) oValue

or

oClass := oValue ASTYPE MyClass

or you can totally replace the local declaration and put it in the IS pattern instead:

IF oValue IS MyClass VAR oClass
? oClass:ToString()
ENDIF

Re: Assignment when a value is of a specific class

Posted: Mon May 19, 2025 4:58 pm
by wriedmann
Hi Chris,
thank you very much!
I was searching this form:

Code: Select all

oClass := oValue ASTYPE MyClass
because when using

Code: Select all

oClass := (MyClass) oValue
and oValue is of another type I will see a runtime exception.
Wolfgang
P.S. this is from a code piece where I need to traverse JSON values