Click here to Skip to main content
15,887,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all,

I have started playing with Roslyn, but have gotten stuck.
(This is just a proof of concept, and I'm also not 100% sure if I am going about this the right way.)

I am trying to create a method that will calculate values based on a dynamic calculation string, by utilizing the values from a passed DataGridViewRow. At the end of the day, this must be able to run 5 calculations per DataGridViewRow (500 row in total) without taking forever.

--

I have created a simple object to carry the values:

C#
public class calculationObject
{           
   public string calc { get; set; 
   public DataGridViewRow row { get; set; }
}



The string calculation is: (it is stored as a string in my calcutationObject):
C#
//just a test calculation using decimal parsing

(decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString()



The methods I have created to do the calculation:

C#
public static MetadataReference hostReference;
public static ScriptEngine engine;
public static void setup()
{
   hostReference =
   new AssemblyFileReference(typeof(frmImportData).Assembly.Location);
   engine = new ScriptEngine(
   importedNamespaces: new[] { "System", "System.Text", "System.Windows.Forms" });
}

public static string trail(calculationObject co)
{
   setup(); /*create Roslyn objects*/

   var code = @"DataGridViewRow row = " + co.row + @";
   int test = " + co.calc + @"; test.ToString();";

   var session = Session.Create();
   return engine.Execute<string>(code, session);
}



I call these methods as such:
C#
calculationObject co = new calculationObject();
co.calc = cal;
co.row = dgvDatabase.Rows[0];

string test = trail(co);


--

The problem I now seem to have, is that the DataGridViewRow row are not being referenced properly by the trail method. The "code" var's value is:

C#
DataGridViewRow row = DataGridViewRow { Index=0 };
int test = (decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) + decimal.Parse(row.Cells["productBasePrice"].Value.ToString()) ).ToString(); test.ToString();


--

So I guess, what I am trying to figure out, is how to reference the col.row object in the "code" var, in order to have the calculation utilize that values.


Thank you!
Posted
Updated 4-Jul-13 9:24am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900