Solved

Eval a string in C#

Posted on 2004-08-11
10
20,692 Views
Last Modified: 2012-06-27
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>
0
Comment
Question by:hendridm
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:jjacksn
ID: 11774908
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() );
          }
     }
}
0
 
LVL 5

Assisted Solution

by:jjacksn
jjacksn earned 50 total points
ID: 11774919
0
 
LVL 5

Assisted Solution

by:jjacksn
jjacksn earned 50 total points
ID: 11774954
After reading around, I don't think there is an equivelent to the JScript eval command.  

http://www.dotnet247.com/247reference/msgs/6/31515.aspx.  If you want to literally execute the strings, it looks like you will have to compile assemblies at run time.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11775000
theres a more recent thread (from yesterday in fact) http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21088360.html

System.Web.UI.DataBinder.Eval() is what you want.
0
 
LVL 8

Author Comment

by:hendridm
ID: 11775017
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 25 total points
ID: 11775043
for that you would be best off to use reflections ...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11783603
Are you talking about using Inline code with ASP.NET?

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11783683
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
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11785062
for the above you could also use FindControl() ...
0
 
LVL 20

Accepted Solution

by:
ihenry earned 175 total points
ID: 11803153
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now