This forum is meant for questions and discussions about the X# language and tools
wriedmann
Posts: 4017 Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy
Post
by wriedmann » Mon May 19, 2025 10:45 am
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
Chris
Posts: 5466 Joined: Thu Oct 08, 2015 7:48 am
Location: Greece
Post
by Chris » Mon May 19, 2025 1:15 pm
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
wriedmann
Posts: 4017 Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy
Post
by wriedmann » Mon May 19, 2025 4:58 pm
Hi Chris,
thank you very much!
I was searching this form:
because when using
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