试图让VB.NET调用IronPython来增加VB变量

时间:2017-11-01 00:00:52

标签: vb.net ironpython

无法通过VB调用IronPython来返回递增的整数。 返回的值与传递给IronPython的值相同。

我希望我的VB.NET windows窗体应用程序将变量123传递给IronPython,让IronPython将10添加到变量中,然后让VB获取它。 但是,它仍然会回来123.

Imports System
Imports System.IO

Imports Microsoft.Scripting
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting


Public Class Form1
     Private Sub Button_GO_Click(sender As Object, e As EventArgs) Handles Button_GO.Click
         ' HELLO WORLD...
         ' bring up an IronPython runtime 
        dim engine = Python.CreateEngine()
        dim  scope = engine.CreateScope()  

        ' create a source tree from code '
        dim source =  engine.CreateScriptSourceFromString("print 'hello from python'")
        ' run the script in the IronPython runtime'
        source.Execute(scope)

        ' MODIFY VARIABLE: BUG:  returns 1 instead of 11'
        engine = Python.CreateEngine()
        scope = engine.CreateScope()  

        ' create a Python variable "i" with the value 1'            
        scope.SetVariable( "i", 1 )

        ' this script will simply add 1 to it '
        source = engine.CreateScriptSourceFromString( "i += 10" )

        ' pull the value back out of IronPython and display it:'
        dim response = scope.GetVariable("i")
        debug.print(     response ) 
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我修好了。看哪....

Imports System
Imports System.IO

Imports Microsoft.Scripting
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting


Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' VB SENDS A VARIABLE TO IRONPYTHON, WHICH INCS AND RETURNS IT......             http://www.chrisumbel.com/article/scripting_ironpython_dlr
       '/* bring up an IronPython runtime */  
        dim engine = Python.CreateEngine()
        dim  scope = engine.CreateScope()  




        ' create a Python variable "i" with the value 1         
        scope.SetVariable( "i", 123 )



        ' this script will simply add 1 to it 
        dim source = engine.CreateScriptSourceFromString( "i += 10" )

        '/* run the script in the IronPython runtime */  
        source.Execute(scope)


       '     /* pull the value back out of IronPython and display it */
            Console.WriteLine(scope.GetVariable("i").ToString())

End Sub

结束班