Unable to cast a string to int32?

Jun 21, 2013 at 3:11 PM
Hi, I am using the Flee library and its really easy to set up and use. However, I have hit a problem in that the library currently doesn't allow me to convert a string to an integer using the CAST method. The expression I'm trying to evaluate is:

cast(xyz, int) * 20

where xyz is a string data type set to "29". When I enter this, the compiler fires the ResolveVariableType handler for xzy which returns a string data type and then it generates the exception:

Ciloci.Flee.ExpressionCompileException: CastElement: Cannot convert type 'String' to 'Double' at Ciloci.Flee.ExpressionElement.ThrowCompileException(String messageKey, CompileExceptionReason reason, Object[] arguments) at Ciloci.Flee.CastElement.ThrowInvalidCastException() at Ciloci.Flee.Cas....

Am I doing something wrong or is there a setting I've missed? If this is a limitation of Flee's CAST statement, can I cast the value any other way (I'm using vb.net)....

Thanks

Dom
Jul 1, 2013 at 1:00 PM
Hey Dom,

in .net you cannot cast a string to an int either,since Flee is based on the .net core,it cant handle that. You could use int.Parse tho :) flee lets you import any .net lib and use any method in any class.

Hope that helps you.

Yoni
Sep 12, 2013 at 3:12 PM
I'm having a similar problem. I get the exception "CastElement: Cannot convert type 'Int32' to 'String'" when trying to cast an integer as a string.

potpockey, I've tried importing Convert and am not able to make it work. After adding Imports.AddType(typeof(Convert) and replacing cast with Convert.ToString(...), I get the exception "IdentifierElement: Could not find a field/property/variable with the name 'Convert'"

An example of what you're describing for int.Parse would go a long way to helping others, including myself, understand how to import and make use of other .Net data types.
Sep 18, 2013 at 2:22 PM
if you imported convert you access its methods directly -- dont write Convert.ToString,but just ToString(yourintvariable)
Dec 13, 2013 at 3:14 PM
Edited Dec 13, 2013 at 3:15 PM
Works great for casting to other types too...
            context.Imports.AddType(typeof(Convert));
            context.Imports.AddType(typeof(Math));
            context.Imports.AddVariable("thisValue", "-100");
            IDynamicExpression e = context.CompileDynamic("ABS(ToDouble(thisValue))");
            double d = (double)e.Evaluate();
Dec 13, 2013 at 3:22 PM
I did get it to work following potpockey's advice of doing "ToString(...)" instead of "Convert.ToString(...)". I was also able to import it with a "Convert" namespace via "Imports.AddType(typeof(Convert), "Convert")" which then enabled me to do Convert.ToString(...) within my expression. Thank you for the help potpockey!