Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: How to pass short int from script to C# methods

$
0
0
Hi James2015Li,

The native value of a JavaScript number is double-precision floating-point. When you pass such a value to a .NET method, ClearScript converts it to one of the following managed types, choosing the first one that results in no numeric data loss:
  • System.Int32
  • System.Int64
  • System.Single
  • System.Double
In your sample above the argument 123 is converted to System.Int32, which is not implicitly convertible to System.Int16, so the call fails. However, ClearScript provides a helper function you can use in this scenario:
engine.AddHostObject("host", new HostFunctions());
engine.Execute("Me.SetValue(host.toInt16(123));");
The HostFunctions class includes methods for converting script numbers to most managed numeric types. Unfortunately at the moment there's no way to change the default conversion algorithm.

Good luck!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>