Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm currently trying to create a dictionnary that looks like this :

Dim dict As New Dictionary(Of Integer, Action)
dict.Add(1, MySubFunction1)
dict.Add(2, MySubFunction2)
 Public Sub MySubFunction1()
    'do something, return nothing'
End Sub
Public Sub MySubFunction2()
    'do something, return nothing'
End Sub

Problem is, I cannot use Action with sub function like i saw in c#. Shoud i replace "Sub" by "Function" and always return something, like this :

Public Function MySubFunction1()
    'do something'
    Return True
End Function
Public Function MySubFunction2()
    'do something'
    Return True
End Function

Or is there any better way ?

Action and Sub is the right combination.
But unlike in c# you cannot use just the method name as a delegate, you need to use AddressOf:

Dim dict As New Dictionary(Of Integer, Action)
dict.Add(1, AddressOf MySubFunction1)
dict.Add(2, AddressOf MySubFunction2)
dict(1).Invoke
Public Sub MySubFunction1()
    Console.WriteLine("Test")
End Sub
Public Sub MySubFunction2()
End Sub
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.