This project has moved and is read-only. For the latest updates, please go here.

Decimal values problems

Mar 12, 2014 at 2:37 PM
Edited Mar 12, 2014 at 2:38 PM
Hi,

There is a problem with marshaling of decimal values. Following code does not work. Type mismatch error
using(var scripter = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging))
{

    decimal num1 = 3;
    decimal num2 = 2;
    scripter.Script.num1 = num1;
    scripter.Script.num2 = num2;
    Console.WriteLine(scripter.Evaluate("num1+num2"));

}
Regards,

Igor
Coordinator
Mar 12, 2014 at 3:39 PM
Hello Igor,

VBScript doesn't support System.Decimal, but because it's a valid COM automation type, it stores it correctly and can return it to the host without data loss.

This is by design. If you'd like to perform operations with decimal values, you'll have to call a host method.

Thanks!
Mar 12, 2014 at 3:55 PM
Can you give me example?
Mar 12, 2014 at 4:01 PM
Using of CurrencyWrapper resolve this issue.
Coordinator
Mar 12, 2014 at 4:53 PM
Hi Igor,

Can you give me example?

Sure. One thing you could do is use a host method to operate on decimal values:
scripter.Script.add = new Func<decimal, decimal, decimal>((a, b) => a + b);
Console.WriteLine(scripter.Evaluate("add(num1, num2)")); 
Another possibility might be to use a host method to cast decimal values to a type that VBScript can deal with:
scripter.Script.toDouble = new Func<decimal, double>(value => (double)value);
Console.WriteLine(scripter.Evaluate("toDouble(num1) + toDouble(num2)"));
Good luck!
Mar 12, 2014 at 5:42 PM
Edited Mar 12, 2014 at 5:43 PM
It's not solution for our legacy application, we can't fix all customer scripts.
Anyway thanks a lot.
Coordinator
Mar 12, 2014 at 7:18 PM
What VBScript data type do the scripts use for which you're trying to provide decimal values? Is it currency?
Mar 12, 2014 at 8:19 PM
Yes, it's a currency
Coordinator
Mar 12, 2014 at 9:03 PM
Edited Mar 13, 2014 at 5:05 AM
We'll add decimal <-> currency marshaling to ClearScript. Thanks!