4

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

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.