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# |
Copy Code
|
---|---|
public class Product { public int Id { get; set; } public String Name { get; set; } public int Qnt { get; set; } public double Price { get; set; } } |
C# |
Copy Code
|
---|---|
//Initialize C1CalcEngine instance of type VBAEngine C1CalcEngine _calcEngine = new C1CalcEngine(); |
C# |
Copy Code
|
---|---|
//Bind C1CalcEngine to data collection 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# |
Copy Code
|
---|---|
//Assign the expression to be calculated to C1CalcEngine _calcEngine.Expression = "=Sum([Qnt]*[Price])"; //Invoke the TryEvaluate method of C1CalcEngine to calculate the expression var res = _calcEngine.TryEvaluate(out object result) ? result.ToString() : _calcEngine.GetErrors().FirstOrDefault()?.FullMessage ?? "";//To Evaluate the expression //Display the expression evaluation result Console.WriteLine("Total Price of the Products: " + res); |