On Thu, 26 Jul 2001, Vikram Rajan wrote:
> hi,
>
> A snippet from my Java program ...
>
> Interp interp = new Interp();
> String str = new String("this");
In Java code, you should write this as:
String str = "this";
> TclObject sTObject = ReflectObject.newInstance(interp, new
> String().getClass(), str);
Be careful. You should never call obj.getClass() in the
second argument to ReflectObject.newInstance(). In this
case it would not break things but in other it might.
Use String.class as the second argument, it is much
safer. Besides, you are allocating an object for no
reason.
> interp.setVar("value", null, sTObject, TCL.GLOBAL_ONLY);
> interp.eval(script);
> String s = (String)ReflectObject.get(interp, sTObject);
> System.out.println("program output = " + s);
>
> The script ....
>
> set $value [java::new String "that"]
> puts "script output = $value"
>
> ********* OUTPUT *********
> script output = this
> program output = this
>
>
> The script does not change the value of the string variable. This seems to
> happen every time I try to use the constructor to change the value of Java
> object that is set as a interpreter variable.
> What could be going wrong?
It looks like you want to set the value variable to "this" and
then change the variables contents. If you do that, you will
need to query the new value from the Tcl interp or look at
the interp result. Your code just sets the value of a Tcl
var and then gets the value from the same TclObj. The Tcl
interp will create a new TclObj when the script changes
the "value" variable. Use Interp.getVar() and you should
be ok.
cheers
Mo
|