AddressOf. This VB.NET operator enables delegate functions. With it we reference a method and use it as the implementation for a delegate.
Operator syntax. We use the name of a function and specify it where a delegate is required. We can use AddressOf to reference a function instead of a lambda.
Example. This program introduces the IsThreeLetters Function, which receives a String and returns a Boolean. This Function is used as a delegate. In Main we create a List and add 3 values to it.
Then The List.Find Function is used to find the first 3-letter String in the List instance.
Module Module1
Function IsThreeLetters(ByVal value As String) As Boolean
Return value.Length = 3
End Function
Sub Main()
Dim list As List(Of String) = New List(Of String)
list.Add("mouse")
list.Add("horse")
list.Add("dog")
Dim value As String = list.Find(AddressOf IsThreeLetters)
Console.WriteLine(value)
End Sub
End Moduledog
Discussion. In the C# language, you do not need AddressOf. Instead, you can use the method name directly as the delegate. The AddressOf operator may be less confusing.
Info AddressOf introduces the context in which the method name should be referenced. The method name is just used to acquire a memory address.
Summary. The AddressOf operator references an existing Function and turns it into a delegate. We do not need to use lambda expression syntax. We can use existing functions.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 20, 2022 (rewrite).