Using Eval to set dynamic object names

Discussion in 'ASP General' started by webteam@gt4web.co.uk, Jul 13, 2006.

  1. Guest

    I've ran into a little trouble with Eval, hoping someone can point it
    out to me

    This code works :

    var_ktml = "textarea2"
    Set ktml_textarea2 = new ktml4
    Eval("ktml_" & var_ktml).Init var_ktml

    This code doesn't :

    var_ktml = "textarea2"
    Set Eval("ktml_" & var_ktml) = new ktml4
    Eval("ktml_" & var_ktml).Init var_ktml


    Can anyone point out the error of my ways ?

    Cheers,
    Mark
    , Jul 13, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > I've ran into a little trouble with Eval, hoping someone can point it
    > out to me
    >
    > This code works :
    >
    > var_ktml = "textarea2"
    > Set ktml_textarea2 = new ktml4
    > Eval("ktml_" & var_ktml).Init var_ktml
    >
    > This code doesn't :
    >
    > var_ktml = "textarea2"
    > Set Eval("ktml_" & var_ktml) = new ktml4
    > Eval("ktml_" & var_ktml).Init var_ktml
    >
    >
    > Can anyone point out the error of my ways ?
    >


    Simple, stop using Eval.

    Use an array or even Scripting.Dictionary to collect and lookup objects


    > Cheers,
    > Mark
    >
    Anthony Jones, Jul 13, 2006
    #2
    1. Advertising

  3. wrote:
    > I've ran into a little trouble with Eval, hoping someone can point it
    > out to me
    >
    > This code works :
    >
    > var_ktml = "textarea2"
    > Set ktml_textarea2 = new ktml4
    > Eval("ktml_" & var_ktml).Init var_ktml
    >
    > This code doesn't :
    >
    > var_ktml = "textarea2"
    > Set Eval("ktml_" & var_ktml) = new ktml4
    > Eval("ktml_" & var_ktml).Init var_ktml
    >
    >
    > Can anyone point out the error of my ways ?
    >

    Sure. You see that line that starts with "Eval ... "? That's the problem
    right there*.
    Try this instead:

    dim var_ktml,ktml_objs, ktml_obj
    set ktml_objs = createobject("scripting.dictionary")
    var_ktml = "textarea2"
    Set ktml_obj = new ktml4
    ktml_objs.Item("ktml_" & var_ktml) = ktml_obj


    When you want to use the object:

    Set ktml_obj = ktml_objs.Item("ktml_" & var_ktml)
    ktml_obj.Init var_ktml
    ktml_objs.Item("ktml_" & var_ktml) = ktml_obj

    Bob Barrows
    * http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Jul 13, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,286
    cesarjusto
    Jul 15, 2010
  2. Eric Newton
    Replies:
    3
    Views:
    9,378
    Brock Allen
    Apr 4, 2005
  3. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    538
    Karl Seguin [MVP]
    Jun 16, 2006
  4. wanwan
    Replies:
    3
    Views:
    428
    Alex Martelli
    Oct 14, 2005
  5. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,425
    Bruno Desthuilliers
    Jan 8, 2009
Loading...

Share This Page