Danny Ackerman
Danny Ackerman

Reputation: 997

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();
}

Upvotes: 4

Views: 125

Answers (1)

sstan
sstan

Reputation: 36483

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

Upvotes: 5

Related Questions