Danny Ackerman Danny Ackerman - 4 years ago 112
Vb.net Question

Add function references to array in VB.NET

Is there a way to add references to functions to a list or array in VB.NET? Something like this in JavaScript:

function hello() {
console.log('hello, world!');
}

function test() {
console.log('test');
}

var functionList = [];

functionList.push(hello);
functionList.push(test);

functionList.forEach(function(n) {
n();
}

Answer Source

Sure. You can create a list of Action delegates:

Sub Hello()
    Console.WriteLine("hello, world!")
End Sub

Sub Test()
    Console.WriteLine("test")
End Sub

Sub Main()
    Dim functionList As List(Of Action) = New List(Of Action)()

    functionList.Add(AddressOf Hello)
    functionList.Add(AddressOf Test)

    For Each n As Action In functionList
        n()
    Next
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download