C# eval function?

C

ChAnGePoInT_DaWg

Is there an "eval" function in C#? Here is what I want to do:

I have a lot of controls like this:

<asp:textbox id="books_quantity" runat="server"/>
<asp:textbox id="vcds_quantity" runat="server"/>
<asp:textbox id="dvds_quantity" runat="server"/>

In my script, I want to refer to the names dynamically like this:

eval("books" + "_quantity").Text = 100;

Is it possible in C#? Because in PHP, Javascript.. they all can do this.

Help..
 
G

George Ter-Saakov

Nope.
C# is compiled language.
Compiler must know up front what object you are accessing.
You can use FindControl(name) and then cast it to Textbox.

George.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You can get the functionality in ASP.NET ( it does not matter if in Vb.net
or C# ) but no using an "eval" feature, .NET languages ( not sure for
jscript ) are compiled languages meaning that you cannot "evaluate" an
expression at runtime. That's a big difference between a compiled language
and a interpreted one.

The feature that let you do that in ASP.NET is cause all the controls in the
page are referenced in a collection, named Controls , now it turns out that
each control has a string property named Name that contain the name of the
control, this is assigned by the page parser when the page is first
compiled.
Therefore to get a reference to a control you can do this:

Controls.Find( "control_name");

This return a Control instance which you must cast it back to the correct
type:

TextBox tb = ( TextBox) Controls.Find( "control_name");
tb.Text = "asdfaf";


Hope this help,
 
K

Klaus H. Probst

Create an assembly with JScript.net that does this. C# can't do it natively,
unless you use CodeDOM and generate a temporary in-memory assembly that has
the code to execute the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top