does c# have an eval method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will

    #1

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

    #2
    Re: does c# have an eval method?

    No, there is not.

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

    "Will" <wswelch@comcas t.net> wrote in message
    news:033801c394 ea$ca83a8b0$a30 1280a@phx.gbl.. .[color=blue]
    > 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[/color]


    Comment

    • Steve C. Orr [MVP, MCSD]

      #3
      Re: does c# have an eval method?

      You should be able to use reflection to do this.

      Here's an example:


      --
      I hope this helps,
      Steve C. Orr, MCSD, MVP

      Hire top-notch developers at http://www.able-consulting.com



      "Will" <wswelch@comcas t.net> wrote in message
      news:033801c394 ea$ca83a8b0$a30 1280a@phx.gbl.. .[color=blue]
      > 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[/color]


      Comment

      • Rick Strahl [MVP]

        #4
        Re: does c# have an eval method?


        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


        ----------------------------------
        Making waves on the Web


        "Will" <wswelch@comcas t.net> wrote in message
        news:033801c394 ea$ca83a8b0$a30 1280a@phx.gbl.. .[color=blue]
        > 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[/color]


        Comment

        • John Timney \(Microsoft MVP\)

          #5
          Re: does c# have an eval method?

          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" <wswelch@comcas t.net> wrote in message
          news:033801c394 ea$ca83a8b0$a30 1280a@phx.gbl.. .[color=blue]
          > 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[/color]


          Comment

          Working...