The following edit to your code snippet would give you what you want

Comment out the current SetVariables call
        //env.Globals.SetVariable("name", "Huzaifa");

//Load the Forms assembly and set the variable in your defaultScope obj.
        env.LoadAssembly(typeof(System.Windows.Forms.MessageBox).Assembly);
        defaultScope.SetVariable("name", "Huzaifa");

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Curt Hagenlocher
Sent: Sunday, August 10, 2008 1:32 AM
To: Discussion of IronPython
Subject: Re: [IronPython] System.Scripting.Runtime.UnboundNameException

A hosting API global is not the same as a Python global.  After all, Python 
globals aren't really global; they're scoped to the module you're in.  I can't 
provide sample code offhand, but to do what you want, you'll need to do 
something like the following steps:

1) Create an empty script scope
2) Set the "name" variable in that scope
3) Load your source into that already-created scope instead of creating a new 
one.

There are probably better ways to structure this code, but it looks like you're 
just working out a test program so I won't be pedantic about it.

In this sample code, you also haven't loaded System.Windows.Forms.dll; you can 
do that either through Python with clr.AddReference or from the hosting code 
with env.LoadAssembly.
On Sun, Aug 10, 2008 at 1:22 AM, Huzaifa <[EMAIL PROTECTED]<mailto:[EMAIL 
PROTECTED]>> wrote:

i am trying this code and it is giving me exception
"$exception     {"name 'name' is not defined"}  System.Exception
{System.Scripting.Runtime.UnboundNameException}
"
CODE:

ScriptRuntime env = ScriptRuntime.Create ( );
ScriptScope defaultScope = env.CreateScope ( );
ScriptEngine curEngine = env.GetEngine ("py");
ScriptSource input;

env.LoadAssembly (typeof (System.Diagnostics.Debug).Assembly);
env.Globals.SetVariable("name","Huzaifa");

input = curEngine.CreateScriptSourceFromString (
               @"import clr
from System import *
from System.Windows.Forms import *

MessageBox.Show(name)
", SourceCodeKind.Statements);

input.Execute (defaultScope);


--
View this message in context: 
http://www.nabble.com/System.Scripting.Runtime.UnboundNameException-tp18911599p18911599.html
Sent from the IronPython mailing list archive at Nabble.com.

_______________________________________________
Users mailing list
[email protected]<mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to