Link to home
Start Free TrialLog in
Avatar of hendridm
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(myBarName));


Should output:
<p>Value of string goes here</p>
Avatar of jjacksn
jjacksn

This is not really want you want, but might point you in the correct direction

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(Name) ;
               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.ArgumentOutOfRangeException(Name) ;
               }
          }

          [STAThread]
          static void Main(string[] args)
          {
               foo f = new foo();
               f.Bar = 37;
               object o = GetProperty("Bar", f);
               Console.WriteLine("f.Bar = " + o.ToString() );
          }
     }
}
SOLUTION
Avatar of jjacksn
jjacksn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.Eval() is what you want.
Avatar of hendridm

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
Are you talking about using Inline code with ASP.NET?

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
for the above you could also use FindControl() ...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial