vassilych / cscs

CSCS: Customized Scripting in C#
MIT License
169 stars 49 forks source link

Import and Export Variables from real c# #5

Open michalss opened 6 years ago

michalss commented 6 years ago

Hi, i would like to import value from my selected source once i run the script, somewhere from my code. For example from listview or grid? Like i select something and get value from it and save it as cscs variable before any commands start process. Same for exporting variable, so example, saved some variable from cscs and i want to export it back to my code as variable. Can this be done pls ?

Thx

vassilych commented 6 years ago

Hi, actually it sounds like a cool and useful feature. I am going to implement it and let you know when ready. Thanks, Vassili

michalss commented 6 years ago

Any news on this pls ? I kind of need it :)

vassilych commented 6 years ago

Can you give me an example of kind of C# variables that you will be using in CSCS scripts? The problem is whether they are static or not. If static, ok, then you can mention the scope, like MyClass.MyStaticVariable. But if they are not static, how would you propose to access them?

michalss commented 6 years ago

Simple pass it to executable procedure void script. same once the script ends. And yes static is fine., There is not need to change it dynamically i guess..

image

Something like this, but it can be list of variable or anything else if someone need pass more then 1. And also it could be good to change it from void to Task or make it as function to get return values, for example. Just an idea.. :) Ahh as u can see i was trying to do it with global variable but it is useless once u want to thread it :( Thx

michalss commented 6 years ago

any news pls?

vassilych commented 6 years ago

I am actually still struggling to get an example on how you can use it (I mean what kind of variables you can have in C# code that you want to access later in CSCS?)

michalss commented 6 years ago

well string would be enought. Simple, in void ProcessScript i would like to add variable and pass it to cscs and once script finnish to get variable back from cscs to c#. It is not hard to do it with global varialbe, but as i said threading is a problem...

michalss commented 6 years ago

Any update pls ?

michalss commented 6 years ago

Working on it pls ? Jut let me know if so or i need to make it different way.. Thx

vassilych commented 6 years ago

Hi, I committed it. Main file where it is defined is Statics.cs It has the following variables, that can be updated from the CSCS code:

public static string StringVar = ""; public static double DoubleVar = 0.0; public static bool BoolVar = false; public static int IntVar = 0;

The CSCS functions to use are GetNative("C# variable name") and SetNative("C# variable name", value):

Here is an example in CSCS of the usage:

SetNative("StringVar", "abc"); x = GetNative("StringVar"); print("x=", x);

SetNative("DoubleVar", 123.45); d = GetNative("DoubleVar"); print("d=", d);

Note that how is everything set up, the variables are all defined in the Statics.cs class. You can define there other variables as well, just keep "public static " for them, so that you can access those variables anywhere from your C# code, e.g.:

string x = Statics.StringVar;

Let me know if it works for you.

Cheers, Vassili

michalss commented 6 years ago

Hi,

Can this variables be instancseable pls ? I would like to get return variable per thread..

michalss commented 5 years ago

is there any chance to pls do it ? If so how ?

vassilych commented 5 years ago

Yes, these variables are static. But you can add there a unique id (e.g. just a counter, or a thread id, etc.) to make them unique per thread. E.g.:

name = "StringVar" + someId; SetNative(name, "abc"); x = GetNative(name); print("x=", x);

Wouldn't setting a unique name make the trick?

JasonLWalker commented 4 years ago

I am working on a module that would allow dynamic runtime import/export of variables to and from C# without the need to define and compile them in the Statics.cs file. I already have this working in some of my own projects, but will try to extract it into it's own module and commit it as a pull request soon.

vassilych commented 4 years ago

Hi Jason,

Looks very good! Let me merge it once I am back from my vacation on Sunday.

Thanks, Vassili

On Mon, 5 Oct 2020 at 06:32, JasonLWalker notifications@github.com wrote:

I am working on a module that would allow dynamic runtime import/export of variables to and from C# without the need to define and compile them in the Statics.cs file. I already have this working in some of my own projects, but will try to extract it into it's own module and commit it as a pull request soon.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vassilych/cscs/issues/5#issuecomment-703377446, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE74VFASHVGQ63YN37JJIATSJE43FANCNFSM4FLJPQ7Q .