473,508 Members | 2,380 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 1506
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*****@comcast.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*****@comcast.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.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase ;

public static object GetField(object loObject,string lcProperty)
{
return
loObject.GetType().GetField(lcProperty,wwUtils.Mem berAccess).GetValue(loObje
ct);
}
public static void SetField(object loObject,string lcProperty,object
loValue)
{

loObject.GetType().GetField(lcProperty,wwUtils.Mem berAccess).SetValue(loObje
ct,loValue);
}

You can then call this like so:

(TextBox) tb = wwUtils.GetField(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*****@comcast.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_author_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_author_plug>
----------------------------------------------

"Will" <ws*****@comcast.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
2071
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...
9
8451
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...
1
3104
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...
8
2509
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...
2
2028
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: ...
2
11139
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...
2
1993
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...
2
9902
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
10563
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='<%#...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
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,...
0
7377
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...
0
7489
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...
0
5624
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,...
1
5047
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.