-1

So I've made a function called Something(string text) that needs a string. Now I have a button that onclick does Something(textBox1.Text).

I want the textBox.Text to be able to be something like "test " + (5+2).toString()

Now the function will get ""test " + (5+2).toString()" (for example) as input and it'll treat that like a big string.

What I want it to do is get test 7 and not treat it like one big string.

How would I go about doing that?

Thanks in advance! -Tim

EDIT:

This would be my function:

private void Something(string text)
{
    MessageBox.Show(text);
}

If I then do

Something(textBox1.Text);

When textBox1 looks like

__________________
| "hi " + "test" |
------------------

It will do make a MessageBox that says "hi" + "test" and not hi test.

Tvde1
  • 1,246
  • 3
  • 21
  • 41

2 Answers2

1

You need to compile the string in your textbox and run the code before giving it to your Something-method.

Take a look at Roslyn to easily evaluate the expression from the textbox.

Then you'll get:

string result = await CSharpScript.EvaluateAsync<string>("\"test \" + (5+2).toString()");
Something(result);
Pidon
  • 275
  • 1
  • 6
1

I think you must try in this way. res contains the answer for the arithmetic operation.

 doSomething(string text)
    {            
        DataTable dt = new DataTable();
        string[] operators = { "+", "*", "/", "-" };
        string[] data = text.Split(' ');
        for(int i=0; i< data.Length; i++)
        {
            if((operators.Any(data[i].Contains)))
            {
                MessageBox.Show(data[i].ToString());
                var res = dt.Compute(data[i],"");
            }
        }

    }