Tangible Software Solutions

VB and Python Equivalents


Equivalents were produced with Instant C# (VB to C# converter) and C# to Python Converter.


The closest thing in Python to an abstract method is an empty method:

VB Python
Public MustInherit Class AbstractClass
    Public MustOverride Sub abstractMethod()
End Class
class AbstractClass:
    def abstractMethod(self):
        pass

You can convert VB arrays to Python's 'list'.

Unsized Array

VB Python
Dim myArray() As Integer myArray = []

Sized Array

VB Python
Dim myArray(1) As Integer myArray = [0 for _ in range(2)]

Access Array Element

VB Python
x = myArray(0) x = myArray[0]   

Jagged Array

VB Python
Dim myArray()() As Single = { New Single() {x, y} } myArray = [[x, y]]

Rectangular Array

VB Python
Dim myArray()() As Integer = {
    New Integer() {10, 20, 30, 40},
    New Integer() {50, 60, 70, 80, 90, 100},
    New Integer() {110, 120}
}
myArray = [[10, 20, 30, 40], [50, 60, 70, 80, 90, 100], [110, 120]]

Python does not support VB-style 'ByRef' parameters. All Python parameters are passed by value (if it's a reference, then the reference is passed by value). However, you can wrap the parameter type in another type (we call it 'RefObject').

Here's a simple example:

VB:

Public Sub refParamMethod(ByRef i As Integer)
    i = 1
End Sub

Public Sub callRefParamMethod()
    Dim i As Integer = 0
    refParamMethod(i)
End Sub

Python:

def refParamMethod(self, i):
    i.arg_value = 1

def callRefParamMethod(self):
    i = 0
    temp_ref_i = RefObject(i)
    self.refParamMethod(temp_ref_i)
    i = temp_ref_i.arg_value

# ----------------------------------------------------------------------------------------
# Copyright © 2023 Tangible Software Solutions, Inc.
# This class can be used by anyone provided that the copyright notice remains intact.
#
# This class is used to replicate the ability to have 'ByRef' parameters in Python.
# ----------------------------------------------------------------------------------------
class RefObject:
    def __init__(self, ref_arg):
        self.arg_value = ref_arg

VB Python
Sub casts()
    x = CInt(y)
    x = CSng(y)
    x = CStr(y)
End Sub
def casts(self):
    x = int(y)
    x = float(y)
    x = str(y)

Lists

VB's System.Collections.Generic.List collection and Python's 'list' are very close equivalents.

VB Python
Sub Lists()
    Dim myList As New List(Of Integer)()
    myList.Add(1)
    myList.Insert(1, 2)
    Dim i As Integer = 1
    myList(0) = i
    i = myList(0)
End Sub
def arrayLists(self):
    myList = []
    myList.append(1)
    myList.insert(1, 2)
    i = 1
    myList[0] = i
    i = myList[0]

Dictionaries

VB's System.Collections.Generic.Dictionary collection and Python's 'dict' are very close equivalents.

VB Python
Sub Dictionaries()
    Dim map As New Dictionary(Of String, Integer)()
    Dim s As String = "test"
    map(s) = 1
    Dim i As Integer = map(s)
    i = map.Count
    Dim b As Boolean = map.Count = 0
    map.Remove(s)
End Sub
def hashMaps(self):
    map = {}
    s = "test"
    map[s] = 1
    i = map[s]
    i = len(map)
    b = not map
    map.pop(s)

Python has a single comment format, starting with the '#' symbol. Multiline comments can also be created using triple quoted multiline strings.

VB Python
' single-line comment

foo() ' end-of-line comment

' comment
' over multiple lines
# single-line comment

foo() # end-of-line comment

# comment
# over multiple lines

# using multiline strings to simulate multiline comments:
"""
comment
over multiple lines"""

Local Variable

VB Python
Dim myVar As Integer = 2 myVar = 2  # Python infers the type

Inferred Types

Inferred typing is available in VB, but Python always infers the type:

VB Python
Option Infer On
Dim myVar = 2
myVar = 2

Static Field

In Python, static fields are initialized at the class level:

VB Python
Class FooClass
    Public Shared staticField As Integer = 7
End Class
class FooClass:
    staticField = 7

Instance Field

In Python, instance fields are not listed at the class level and are qualified with the instance object, given by the first instance method parameter (usually named 'self' by convention):

VB Python
Class FooClass
    Public instanceField As Integer = 2
End Class
class FooClass:
    def __init__(self):
        self.instanceField = 2

Local Constant

VB Python
Const myConst As Integer = 2 MY_CONST = 2  # at method level — up to programmers to respect the naming convention and avoid changing it

Class Constant

VB Python
Public Const myConst As Integer = 2 MY_CONST = 2  # at class level — up to programmers to respect the naming convention and avoid changing it

Python constructors are named __init__, but a serious shortcoming of Python is the restriction of allowing only one constructor per class.

VB Python
Class Foo
    Public Sub New()
    End Sub
End Class
class Foo:
    def __init__(self):
        pass

Python and VB both allow default or optional parameters.

VB Python
Public Sub defaultParam(Optional ByVal param As Integer = 0)
    ...
End Sub
def defaultParam(self, param=0):
    ...

Since methods are first class objects in Python delegates are converted easily:

VB Python
Public Class Test
    Public Delegate Sub FooDelegate(ByVal arg As Integer)

    Public Shared Sub static_method(ByVal p As Integer)
        Console.WriteLine("static_method called with arg " & p)
    End Sub

    Public Sub instance_method(ByVal p As Integer)
        Console.WriteLine("instance_method called with arg " & p)
    End Sub

    Public Sub method()
        Dim f1 As New FooDelegate(AddressOf static_method)
        f1(1)

        Dim f2 As FooDelegate = AddressOf instance_method
        f2(2)

        Dim f3 As FooDelegate = Sub(i As Integer) instance_method(i)
        f3(3)
    End Sub
End Class
class Test:
    class FooDelegate:
        def __init__(self, method, instance):
            self._method = method
            self._instance = instance

        def invoke(self, arg):
            if self._instance is None:
                self._method(arg)
            else:
                self._method(self._instance, arg)

    @staticmethod
    def static_method(p):
        print("static_method called with arg " + str(p))

    def instance_method(self, p):
        print("instance_method called with arg " + str(p))

    def method(self):
        f1 = Test.FooDelegate(Test.static_method, None)
        f1.invoke(1)

        f2 = Test.FooDelegate(Test.instance_method, self)
        f2.invoke(2)

        f3 = Test.FooDelegate(lambda i : self.instance_method(i), None)
        f3.invoke(3)

Enums in VB have equivalents in Python using the 'enum' module:

VB Python
Public Enum Simple
    Foo
    Bar
End Enum
from enum import Enum

class Simple(Enum):
    FOO = 0
    BAR = 1

......

VB Python
' value equality with primitives:
Dim i As Integer = 1
Dim j As Integer = 1
Dim valuesIdentical As Boolean = i = j

' identity equality with class types:
Dim f As New Foo()
Dim g As Foo = f
Dim objectsIdentical As Boolean = f = g

' strings:
Dim s1 As String = "abc"
Dim s2 As String = s1
valuesIdentical = s1 = s2
' caution: VB string internment often
' makes this true whenever '==' is true:
objectsIdentical = string.ReferenceEquals(s1, s2)
# value equality:
i = 1
j = 1
valuesIdentical = i == j

# identity equality
f = Foo()
g = f
objectsIdentical = f is g

# strings:
s1 = "abc"
s2 = s1
valuesIdentical = s1 == s2
# caution: Python string internment often
# makes this true whenever '==' is true:
objectsIdentical = s1 is s2

The Python try/except/finally scheme is equivalent to VB's Try/Catch/Finally:

VB Python
Sub exceptionHandling()
    Try
    Catch f As Foo
        Throw f
    Catch e As Bar
        Throw New Bar()
    Finally
    End Try
End Sub
def exceptionHandling(self):
    try:
        pass
    except Foo as f:
        raise f
    except Bar as e:
        raise Bar()
    finally:
        pass

Python doesn't have extension methods, so a VB extension method is just converted to an ordinary Python static method (calls to the method have to be adjusted to static calls using the class name).

VB Python
Public Module ContainsExtension
    <System.Runtime.CompilerServices.Extension> _
    Public Sub ExtensionMethod(ByVal myParam As String)
        ' ...
    End Sub
End Module

Class TestClass
    Sub TestMethod()
        Dim s As String = "abc"
        s.ExtensionMethod()
    End Sub
End Class
class ContainsExtension:
    @staticmethod
    def ExtensionMethod(myParam):
        # ...
        pass

class TestClass:
    def _TestMethod(self):
        s = "abc"
        ContainsExtension.ExtensionMethod(s)
VB Python
If conditionA Then
ElseIf conditionB Then
Else
End If
if conditionA:
    pass
elif conditionB:
    pass
else:
    pass
VB Python
result = If(condition, truePart, falsePart) result = truePart if condition else falsePart

Python does not have indexers, so you must use get/set methods instead:

VB Python
Default Public Property Item(ByVal index As Integer) As Integer
    Get
        Return field(index)
    End Get
    Set(ByVal value As Integer)
        field(index) = value
    End Set
End Property
def get(self, index):
    return field[index]
def set(self, index, value):
    field[index] = value
VB Python
Public Class one
    Protected baseField As Integer = 1

    Public Sub New(ByVal i As Integer)
    End Sub

    Public Overridable Sub baseMethod()
    End Sub
End Class

Public Class two
    Inherits one

    Public Sub New()
        MyBase.New(0)
    End Sub

    Public Sub method()
        MyBase.baseField = 2
        MyBase.baseMethod()
    End Sub
End Class
class one:

    def __init__(self, i):
        self.baseField = 1

    def baseMethod(self):
        pass

class two(one):
    def __init__(self):
        super().__init__(0)

    def method(self):  
        self.baseField = 2
        super().baseMethod()

Defining Interfaces

Interfaces in Python are just classes with empty methods:

VB Python
Public Interface IFooInterface
    Sub method()
End Interface
class IFooInterface:
    def method(self):
        pass

Implementing Interfaces

VB Python
Public Class Foo
    Implements IFooInterface

    Public Sub method()
        ... some code
    End Sub
End Class
class Foo(IFooInterface):
    def method(self):
        ... some code

Expression Lambda

VB Python
myVar = Function(text As String) text.Length myVar = lambda text : len(text)

Multi-statement Lambda

VB Python
myVar = Sub(param1 As Foo, param2 As Bar)
    ' ...multiple statements
End Sub
No direct Python equivalent
VB Python
Dim x As Boolean = y AndAlso z
x = y OrElse z
x = Not y
Dim i As Integer = j Or k
i = j And k
i = j Xor k
x = y and z
x = y or z
x = not y
i = j | k
i = j & k
i = j ^ k

For Loop

VB.NET Python
For i As Integer = 1 To 9
Next i
for i in range(1, 10):
    pass

For Each Loop

VB.NET Python
For Each s As String In StringList
Next S
for s in StringList:
    pass

While Loop

VB.NET Python
Do While condition
Loop

or:

While condition
End While
while condition:
    pass

Do While Loop

VB.NET Python
Do
Loop While condition
loop_condition = True
while loop_condition:
    loop_condition = condition

Loop Until

VB.NET Python (no specific 'loop until' construct)
Do
Loop Until condition
loop_condition = True
while loop_condition:
    loop_condition = not(condition)

Do Until

VB.NET Python (no specific 'do until' construct)
Do Until condition
Loop
while not(condition):
    pass
VB Python
Sub exponentiation()
    x = y ^ z
    ' or:
    x = Math.Pow(y, z)
End Sub

Sub integerDivision()
    ' VB integer division always rounds towards 0:
    Dim i As Integer = -5
    Dim j As Integer = 2
    Dim result As Integer = i \ j   ' result is -2
End Sub

Sub modulusOperator()
    ' VB 'Mod' operator and Python '%' operator are only equivalent for positive numbers:
    Dim i As Integer = 2
    Dim j As Integer = -3
    Dim result As Integer = i Mod j   ' result is 2
End Sub

Sub otherMathOperations()
    x = Math.Abs(y)

    ' see the System.Math class for many more functions:
    x = Math.Cos(y)
    x = Math.E
    x = Math.PI
End Sub
import math

def exponentiation(self):
    x = y ** z

def integerDivision(self):
    i = -5
    j = 2
    result = math.trunc(i / float(j))   # result is -2

    # Python integer division rounds away from 0 when negative:
    result = i / j   # result is -3

def modulusOperator(self):
    i = 2
    j = -3
    result = math.fmod(i, j)   # result is 2

    # Python modulus operator produces a different result:
    result = i % j   # result is -1

def otherMathOperations(self):
    x = abs(y)   # 'abs' is a built-in Python function

    # see the Python 'math' module for many more functions:
    x = math.cos(y)
    x = math.e
    x = math.pi

Python instance methods have a first parameter indicating the instance ('self' is the convention for this parameter and not a keyword), and static methods have the '@staticmethod' decorator.

VB Python
Public Sub instanceMethod()
End Sub

Public Shared Sub staticMethod()
End Sub
def instanceMethod(self):
    pass

@staticmethod
def staticMethod():
    pass
VB Python
Dim s As String = "multiline
    string"
s = """multiline
    string"""
VB Python
Public Class SomeType
    Private IntValue As Integer = 1

    Public Shared Operator +(ByVal X As SomeType, ByVal Y As SomeType) As Integer
        Return X.IntValue + Y.IntValue
    End Operator

    Public Sub OperatorTest()
        Dim o As New SomeType()
        Dim p As New SomeType()
        i = o + p
    End Sub
End Class
class SomeType:
    def __init__(self):
        self._IntValue = 1

    @staticmethod
    def op_add(X, Y):
        return X._IntValue + Y._IntValue

    def OperatorTest(self):
        o = SomeType()
        p = SomeType()
        i = SomeType.op_add(o, p)
VB Python
Private Sub method(ParamArray ByVal args() As String)
    For Each x As String In args
        ... logic for each item in args
    Next x
End Sub
def method(self, *args):
    for x in args:
        ... logic for each item in args

Python does not have properties, so you must use get/set methods instead:

VB Python
Public Property IntProperty() As Integer
    Get
        Return intField
    End Get
    Set(ByVal value As Integer)
        intField = value
    End Set
End Property
def get_int_property(self):
    return intField
def set_int_property(self, value):
    intField = value

Python 3.10 has 'match' syntax, but if/elif/else is used prior to Python 3.10.

VB Python
Select Case pivot
    Case 1
        foo()
    Case 2, 3
        bar()
    Case 4
    Case Else
        ack()
End Select

Python 3.10:

match pivot:
    case 1:
        foo()
    case 2 | 3:
        bar()
    case 4:
        pass
    case other:
        ack()

Prior to Python 3.10:

if pivot == 1:
    foo()
elif pivot == 2 or pivot == 3:
    bar()
elif pivot == 4:
    pass
else:
    ack()

VB static constructors and Python code at the class level serve the same purpose.

VB Python
Class Foo
    Public Shared field As Integer

    Shared Sub New()
        ... logic to set 'field' value
    End Sub
End Class
class Foo:
    field = 0

    ... logic to set 'field' value
VB Python
Dim s As String = initValue
Dim i As Integer = s.IndexOf(y)
i = s.LastIndexOf(y)
i = s.Length
Dim b As Boolean = s.Contains(y)
s = s.Substring(i)
s = s.Substring(i, j)
b = s.EndsWith(y)
b = s.StartsWith(y)
s = s.ToLower()
s = s.ToUpper()
s = s.TrimStart()
s = s.TrimEnd()
s = initValue
i = s.find(y)
i = s.rfind(y)
i = len(s)
b = y in s
s = s[i:]
s = s[i:i + j]
b = s.endswith(y)
b = s.startswith(y)
s = s.casefold()
s = s.upper()
s = s.lstrip()
s = s.rstrip()
VB Python
' checking if 'f' is an instance of type 'Foo':
Dim b As Boolean = TypeOf f Is Foo
# checking if 'f' is an instance of type 'Foo':
b = isinstance(f, Foo)
VB Python
Imports FooNamespace

' type import with alias matching type name:
Imports FooClass = FooNamespace.FooClass

' namespace alias:
Imports fooalias = FooNamespace

' type import:
Imports FooNamespace.BarClass
from FooNamespace import *

from FooNamespace import FooClass

* no equivalent to type or namespace aliases *

* no equivalent to VB type imports without alias matching type name *
VB.NET Python (omitting null handling)
x = LCase(x)
x = UCase(x)
x = Left(x, 2)
x = Right(x, 2)
x = Mid(x, 3)
i = InStr(x, y)
i = InStrRev(x, y)
x = x.casefold()
x = x.upper()
x = x[0:2]
x = x[len(x) - 2:]
x = x[2:]
i = x.find(y) + 1
i = x.rfind(y) + 1
VB.NET Python
Dim myIntegerVar%
Dim myStringVar$
Dim myFloatVar!
Dim myDoubleVar#
myIntegerVar = 0
myStringVar = None
myFloatVar = 0
myDoubleVar = 0
VB Python
Using foo As New FooType()
    ...
End Using
with FooType() as foo:
    ...

Copyright © 2004 – 2024 Tangible Software Solutions, Inc.