C# eval function?

  • Thread starter Thread starter ChAnGePoInT_DaWg
  • Start date Start date
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..
 
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.
 
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,
 
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.
 
Back
Top