Tvde1 Tvde1 - 1 year ago 70
C# Question

Turn textbox text into string with code in it

So I've made a function called

Something(string text)
that needs a
Now I have a button that onclick does

I want the
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!


This would be my function:

private void Something(string text)

If I then do


When textBox1 looks like

| "hi " + "test" |

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

Answer Source

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()");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download