Using Eval to set dynamic object names

W

webteam

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
 
A

Anthony Jones

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
 
B

Bob Barrows [MVP]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top