This quick start guides you through a process of creating an application that uses VBAEngine, binds CalcEngine to data, assign expression to it and evaluate result.
Complete the following steps to see how the result is calculated based on the assigned expression using CalcEngine.
Create a class, say Product, to generate data collection for CalcEngine.
C# |
コードのコピー
|
---|---|
public class Product { public int Id { get; set; } public String Name { get; set; } public int Qnt { get; set; } public double Price { get; set; } } |
C# |
コードのコピー
|
---|---|
//VBAEngine型のC1CalcEngineインスタンスを初期化します。 C1CalcEngine _calcEngine = new C1CalcEngine(); |
C# |
コードのコピー
|
---|---|
//データ コレクションにC1CalcEngine を連結します。 var collection = new Product[] { new Product {Id=1, Name = "Chai", Qnt = 18, Price = 18.1 }, new Product {Id=2, Name = "Pavlova", Qnt = 11, Price = 17.45 }, new Product { Id=3, Name= "Sir Rodney's Marmalade", Qnt = 1, Price = 40 }, new Product { Id=4, Name= "Ipoh Coffee", Qnt = 10, Price = 14.0 }, new Product { Id=5, Name="Mascarpone Fabioli", Qnt = 16, Price = 32.0 }, new Product { Id=6, Name= "Schoggi Schokolade", Qnt = 12, Price = 43.9 }, }; _calcEngine.DataSource = collection; |
Assign a VBA syntax expression to the CalcEngine using the Expression property and invoke the TryEvaluate method to calculate the expression.
C# |
コードのコピー
|
---|---|
//計算する式を C1CalcEngine に代入します。 _calcEngine.Expression = "=Sum([Qnt]*[Price])"; //C1CalcEngine の TryEvaluate メソッドを呼び出して式を計算します。 var res = _calcEngine.TryEvaluate(out object result) ? result.ToString() : _calcEngine.GetErrors().FirstOrDefault()?.FullMessage ?? "";//To Evaluate the expression //式の評価結果を表示します。 Console.WriteLine("Total Price of the Products: " + res); |