-2
class ExeClass
{
    public void p1()
    {
        //Execute String
        string p = "i=i+1;";
        //How To This 
    }
}

main methods

private void Form1_Load(object sender, EventArgs e)
{
    int i = 1;
    ExeClass p1 = new ExeClass();
    p1.p1();
    int j = i;//Now the amount i,j is the equal to 2
}
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • The is no simple `eval` feature in C#. In principle it's possible to use a function evaluation library, or you could embed a scripting language. But you really should rethink your problem to avoid `eval`. – CodesInChaos May 24 '15 at 10:30
  • possible duplicate of [How can I read the properties of a C# class dynamically?](http://stackoverflow.com/questions/4629/how-can-i-read-the-properties-of-a-c-sharp-class-dynamically) – Jeroen Vannevel May 24 '15 at 10:42

1 Answers1

0

Your question is very vague, but I think I understand what it is you are asking.

The variable you are trying to change inside your ExeClass is not defined in there, but locally in the Form1_Load method.
To get it inside the ExeClass.p1 method, you will need to pass it in.
There are a few ways of doing this. But in this case I would recommend that you pass your local variable and then return the changed value.

Returning something from a method changes the method declaration:

public void p1() // Old
public int p1()  // New

This allows you to return a int value from it.
We also need to pass a value in (your local variable), which is done by changing the declaration a bit more:

public int p1(int value) { ...

Now, we got the value passed in to the p1 method, and we can change it:

public int p1(int value) {
  value += 1;
  return value;
}

Now when we call the function, the value will be returned, and you need to catch it:

private void Form1_Load(object sender, EventArgs e)
{
  int i = 1;
  ExeClass p1 = new ExeClass();
  int j = p1.p1(i); // See that we pass the `i` variable in here and that we catch the return value in the `j` variable.
  Console.WriteLine("Value is now {0}", j); // Will print : "Value is now 2"
}

Edit:

If you are asking if there is a way to execute c# code from a string, then you can take a look at this thread for a bit more info, but if you can avoid it, I would recommend to not do that...

Community
  • 1
  • 1
Jite
  • 5,761
  • 2
  • 23
  • 37
  • Hi I'm sorry that I said My problem is not computing I just wanted to take a simple example to show My problem is when the user removes data – mojtaba khooshrou May 24 '15 at 11:09
  • Before deletion Classes are activated to check that the data has not been used If you use the error For this error, there is a column in which data Contact I have to run. There is dynamic code The problem is that a lot of code used And because of the class I wrote code – mojtaba khooshrou May 24 '15 at 11:09
  • In fact, rather than computing code sits ModelState.AddModelError("Name", "this is used"); – mojtaba khooshrou May 24 '15 at 11:12