Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community Forums[eol][evl]Handling exception in casting types
https://www.eclipse.org/forums/index.php/mv/msg/264702/763322/#msg_763322
I have a class with a <<value>> string field. The purpose of this field is to receive any input data (integers, booleans, reals and strings) and then casting to the desiredtype (like Integer, Boolean, Real ...).
The casting (conversion) of this <<value>> field is happening in a .evl file.
In EVL i'm trying to know if the content of <<value>> field is consistent with the choosen type.
for instance
<<value>> field | choosen type
true boolean It's ok!
Today Integer It isn't ok because ''today'' is not a integer
To do that, I've tried this (to convert from String to Integer)
check: self.valor.asInteger().isKindOf(Integer)
when <<value>> is an Integer, this works well but it isn't this piece of code raises an exception and I couldn't find a way to handling exception in eol or evl.
I'd appreciate any help.
Thanks in advance!
]]>Paulo Alexandre2011-12-09T15:09:36-00:00Re: [eol][evl]Handling exception in casting types
https://www.eclipse.org/forums/index.php/mv/msg/264702/763419/#msg_763419
The code you have written would not check the validity of the string. If
asInteger() is successful it will, per definition, return an integer. So
calling isKindOf (Integer) on it is redundant!
You will probably need to build a helper class of your own to provide
appropriate testing and conversion methods. Check out the 'tools'
concept in the Epsilon book.
Cheers,
Steffen
On 09/12/2011 15:09, Paulo Alexandre wrote:
> hi everybody
>
> I have a class with a <<value>> string field. The purpose of this
> field is to receive any input data (integers, booleans, reals and
> strings) and then casting to the desiredtype (like Integer, Boolean,
> Real ...).
>
> The casting (conversion) of this <<value>> field is happening in a
> .evl file.
>
> In EVL i'm trying to know if the content of <<value>> field is
> consistent with the choosen type.
>
> for instance
> <<value>> field | choosen type true boolean
> It's ok!
> Today Integer It isn't ok because ''today'' is
> not a integer
>
> To do that, I've tried this (to convert from String to Integer)
>
> check: self.valor.asInteger().isKindOf(Integer)
>
> when <<value>> is an Integer, this works well but it isn't this piece
> of code raises an exception and I couldn't find a way to handling
> exception in eol or evl.
>
> I'd appreciate any help.
>
> Thanks in advance!
>
>]]>Steffen Zschaler2011-12-09T17:11:07-00:00Re: [eol][evl]Handling exception in casting types
https://www.eclipse.org/forums/index.php/mv/msg/264702/763692/#msg_763692
Steffen is right: there is no way to check that it's really an integer from pure EVL. In Java you'd normally try to use Integer.valueOf and then check for a NumberFormatException, but we don't have anything for catching exceptions in EVL.
If performance is not an issue (you only have a few of those strings), you could approximate it with a regex. Since it's a String, you could use the matches method:
self.string.matches("[0-9]+")
However, this simple regex would have to be compiled again and again, and it wouldn't take into account all the possible variations for a Java integer literal.
Cheers,
Dimitris]]>Dimitris Kolovos2011-12-10T10:55:37-00:00Re: [eol][evl]Handling exception in casting types
https://www.eclipse.org/forums/index.php/mv/msg/264702/763890/#msg_763890
I really thank you for all the support!
I'll try those ideas and pieces of code and I'll give you a feedbacK!
Thank you very much!!!!!]]>Paulo Alexandre2011-12-10T20:41:53-00:00Re: [eol][evl]Handling exception in casting types
https://www.eclipse.org/forums/index.php/mv/msg/264702/778723/#msg_778723
Paulo Alexandre2012-01-13T17:48:10-00:00