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
Solved

Eval a string in C#

Posted on 2004-08-11
10
20,700 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 51
2 questions 10 29
application restarter  - for an exe that hangs 9 30
The control parameters are not inputing the value when displaying in a report viewer 13 26
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

765 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