473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

does c# have an eval method?

in javascript or the like, when you wish to evaluate a
string you use eval() for instance if you had five text
boxes named textbox1, textbox2, etc. you would write
something like:

for (x=1;x<5;x++) {
eval(textbox + x).text = "item: " + x;
}
how can you do a similar thing in C#? I cannot believe
that there is no eval method. That would just be
rediculous. Thanks in advance for help! :)
Will
Nov 17 '05 #1
4 1521
No, there is not.

99% of the time you can code around it - and this type of coding is usually
more efficient anyway.

"Will" <ws*****@comcas t.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
in javascript or the like, when you wish to evaluate a
string you use eval() for instance if you had five text
boxes named textbox1, textbox2, etc. you would write
something like:

for (x=1;x<5;x++) {
eval(textbox + x).text = "item: " + x;
}
how can you do a similar thing in C#? I cannot believe
that there is no eval method. That would just be
rediculous. Thanks in advance for help! :)
Will

Nov 17 '05 #2
You should be able to use reflection to do this.

Here's an example:
http://www.codeproject.com/useritems/evaluator.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"Will" <ws*****@comcas t.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
in javascript or the like, when you wish to evaluate a
string you use eval() for instance if you had five text
boxes named textbox1, textbox2, etc. you would write
something like:

for (x=1;x<5;x++) {
eval(textbox + x).text = "item: " + x;
}
how can you do a similar thing in C#? I cannot believe
that there is no eval method. That would just be
rediculous. Thanks in advance for help! :)
Will

Nov 17 '05 #3

No there's no eval method unfortunately but you can use Reflection in most
cases to evaluate property expressions or call expressions on objects
dynamically.

eval(textbox + x).text = "item: " + x;

Here's one of the wrappers I use for this sort of thing:

public const BindingFlags MemberAccess =
BindingFlags.Pu blic | BindingFlags.No nPublic |
BindingFlags.St atic | BindingFlags.In stance | BindingFlags.Ig noreCase ;

public static object GetField(object loObject,string lcProperty)
{
return
loObject.GetTyp e().GetField(lc Property,wwUtil s.MemberAccess) .GetValue(loObj e
ct);
}
public static void SetField(object loObject,string lcProperty,obje ct
loValue)
{

loObject.GetTyp e().GetField(lc Property,wwUtil s.MemberAccess) .SetValue(loObj e
ct,loValue);
}

You can then call this like so:

(TextBox) tb = wwUtils.GetFiel d(this,"textbox " + x);
string text = tb.Text;

This works as long as the the field or property in question is not marked as
Private as reflection requires external visibility in order to get at the
types. The same sort of thing works for Properties...

Hope this helps,

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web
"Will" <ws*****@comcas t.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
in javascript or the like, when you wish to evaluate a
string you use eval() for instance if you had five text
boxes named textbox1, textbox2, etc. you would write
something like:

for (x=1;x<5;x++) {
eval(textbox + x).text = "item: " + x;
}
how can you do a similar thing in C#? I cannot believe
that there is no eval method. That would just be
rediculous. Thanks in advance for help! :)
Will

Nov 17 '05 #4
jscript has one, you could always import the approapriate jscript lib and
use that

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_auth or_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_autho r_plug>
----------------------------------------------

"Will" <ws*****@comcas t.net> wrote in message
news:03******** *************** *****@phx.gbl.. .
in javascript or the like, when you wish to evaluate a
string you use eval() for instance if you had five text
boxes named textbox1, textbox2, etc. you would write
something like:

for (x=1;x<5;x++) {
eval(textbox + x).text = "item: " + x;
}
how can you do a similar thing in C#? I cannot believe
that there is no eval method. That would just be
rediculous. Thanks in advance for help! :)
Will

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2081
by: Phil Powell | last post by:
I have this line in my code: eval('$field->' . $val . '(' . $max . ',' . $errDisplayArray . ');'); where $field is a predefined class instance, $val is a string containing the name of an existing method within the class, and $errDisplayArray will be an array element containing text that goes appropriately into the class method whose name is in $val.
9
8459
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select values, this one is a simplified version where its passed. function checkIt(selVal){ valueInDec1 = eval(selVal); //do some calculations here with valueInDec1 }
1
3124
by: Mad Scientist Jr | last post by:
Is there a native .NET equivalent of the Microsoft Script Control, particularly the Eval method? See here: http://www.devx.com/vb2themax/Tip/18773 It works great - it even compares string expressions. I just would prefer a native .NET version over COM if at all possible. Sample code:
8
2535
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
2
2039
by: dm_dal | last post by:
I have a control on my webform that I am binding to a dataset. The issue is, the field value in the dataset is encrypted and I am trying to decrypt it during the binding process: Example: <asp:Lable ID="myLable Text='<%# myComponent.Decrypt(DataBinder.Eval(myDataSet, "Tables.DefaultView..MyField")) '></asp:Label>
2
11163
by: Oleg Ogurok | last post by:
Hi all, I have a DatePicker ASP.NET control that allows users to enter date (e.g. a calendar control). The control has the following property public DateTime Value { get; set; } I've placed the control into a datagrid now I'm trying to bind values from database to the datagrid.
2
1997
by: Girish | last post by:
Hello all, Im wondering why my OnItemDataBound gets fired twice here. I got this sample code from somewhere online.. and when I put a break point in the method... it hits the method twice for each row rendered... Any insights?? Thanks! Girish
2
9912
by: Jeff | last post by:
hey asp.net 2.0 (C'#) In the code behind file I have this method: public String AddBR(Object param) { String text = (String) param; return text; }
3
10573
by: Kevin Blount | last post by:
I'm putting a radG:GridTemplateColumn together (which is probably irelevant), and within it I'm using a Label, as so: <asp:Label ID="defaultDescription" runat="server" Text='<%# Eval("description") %>'></asp:Label> Fo this Label, I'd like to only show the first 50 chars of the "description", but I've no idea how to change the Eval to do this.. is there a way?
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.