Assignment when a value is of a specific class

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 4018
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Assignment when a value is of a specific class

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 5471
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Assignment when a value is of a specific class

Post 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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 4018
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Assignment when a value is of a specific class

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply