Search

Search:

Namespace:

Search Result
.

Public Class HelloWorldVB

.
    Public Overridable Function HelloWorld(ByVal name As String) As String
.
    Dim helloWorld As New HelloWorldVB()
.
    Console.WriteLine(helloWorld.HelloWorld("Maurice"))
.

The following code is the IronPython code I added to the file HelloWorld.py.

.

from IronPythonTest2 import HelloWorldVB

.

class HelloWorldIronPython(HelloWorldVB):

.
    def HelloWorld(self, name):
.

Basically this code adds a reference to the IronPythonTest2 assembly, this is my console application containing the previous VB code. Next it imports the HelloWorldVB type from the assembly. Now it know about the type we can go ahead and derive from that type and create a new one. This exactly what the line class HelloWorldIronPython(HelloWorldVB) does, it creates a new type called HelloWorldIronPython. Next we override the HelloWorld function and return a different welcome string. A few things to note here: Python uses indentation to specify blocks, so the function being indented from the class is a part of that and the line of code with the return is indented from the def that defines the function so it is part of the function body. Another interesting thing is the self parameter. Every function in Python is passed a reference to itself as the first parameter. Sort of like the implicit Me in VB except a bit more obvious.

.

To make sure the HelloWorld.py file is available to the console application I have set the Copy to Output Directory to Copy always.

.

Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")

.

Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")

.

helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)

.

Console.WriteLine(helloWorld.HelloWorld("Maurice"))

.
        Dim helloWorld As New HelloWorldVB()
.
        Console.WriteLine(helloWorld.HelloWorld("Maurice"))
.
        Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
.
        Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
.
        helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
.
        Console.WriteLine(helloWorld.HelloWorld("Maurice"))