hendridm
asked on
Eval a string in C#
How do I evaluate a string in C#? Example:
string myVarName = "sTest";
eval("string "+myVarName+" = \"<p>Value of string goes here</p>\"");
Response.Write(eval(myBarN ame));
Should output:
<p>Value of string goes here</p>
string myVarName = "sTest";
eval("string "+myVarName+" = \"<p>Value of string goes here</p>\"");
Response.Write(eval(myBarN
Should output:
<p>Value of string goes here</p>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
theres a more recent thread (from yesterday in fact) https://www.experts-exchange.com/questions/21088360/How-to-evaluate-a-variable-value.html
System.Web.UI.DataBinder.E val() is what you want.
System.Web.UI.DataBinder.E
ASKER
Ok, thanks. To be more specific, is there a way to do this?
string myVar = "Literal1";
Literal1.Text = "Lorem Ipsum";
Is there a way to programmatically specify 'Literal1', like so:
string myVar = "Literal1";
<name of variable in 'myVar'>.Text = "Lorem Ipsum";
If not, no problem. You essentially answered my question anyway.
string myVar = "Literal1";
Literal1.Text = "Lorem Ipsum";
Is there a way to programmatically specify 'Literal1', like so:
string myVar = "Literal1";
<name of variable in 'myVar'>.Text = "Lorem Ipsum";
If not, no problem. You essentially answered my question anyway.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you talking about using Inline code with ASP.NET?
Bob
Bob
Something like this:
(1) Define a page-level variable in public variable in the code-behind:
Public var_value As Integer = 5
(2) Use the HTML design to use the following inline code:
<p><%=var_value%></p>
The page will display 5 within the block.
Bob
(1) Define a page-level variable in public variable in the code-behind:
Public var_value As Integer = 5
(2) Use the HTML design to use the following inline code:
<p><%=var_value%></p>
The page will display 5 within the block.
Bob
for the above you could also use FindControl() ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
using System;
using System.Reflection ;
namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private class foo {
private int bar;
public int Bar {
get { return bar; }
set { bar = value ; }
}
public foo() {}
}
static object GetProperty(string Name, object Obj) {
PropertyInfo p = Obj.GetType().GetProperty(
if(p!=null) {
if(p.CanRead) {
return p.GetValue(Obj, null) ;
} else {
throw new System.Exception(Name + " is a WriteOnly property cannot read value") ;
}
} else {
throw new System.ArgumentOutOfRangeE
}
}
[STAThread]
static void Main(string[] args)
{
foo f = new foo();
f.Bar = 37;
object o = GetProperty("Bar", f);
Console.WriteLine("f.Bar = " + o.ToString() );
}
}
}