Solved

Eval a string in C#

Posted on 2004-08-11
10
20,695 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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

11 Experts available now in Live!

Get 1:1 Help Now