?
Solved

Eval a string in C#

Posted on 2004-08-11
10
Medium Priority
?
20,711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 total points
ID: 11774919
0
 
LVL 5

Assisted Solution

by:jjacksn
jjacksn earned 150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 75 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 525 total points
ID: 11803153
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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