Predicate | Action | Converter | Comparison

 

Predicate (predicate takes 1 input of any type , return boolean )


Private Sub btnGeneric_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGeneric.Click

Dim strArray As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim result As List(Of Integer) 'anonymous delegate
result = New List(Of Integer)()

iGen = New Generic(AddressOf delMethod1)

Dim iPredicate As New Predicate(Of Integer)(AddressOf FindMax) 'predicate delegate address =findMax

Dim fresult As Integer() = Array.FindAll(strArray, iPredicate)

Dim strResult As String = Nothing

For j As Integer = 0 To fresult.Length - 1
strResult += fresult(j).ToString & " ,"

Next
Me.lblPath.Text = strResult.Substring(0, strResult.Length - 1)
'predicate takes 1 input of any type , return boolean

End Sub

 

Private Shared Function FindMax(ByVal i As Integer) As Boolean
Return i <= 8

End Function


 

Action (Take one parameter of any type without return anything )

 

Private Sub btnAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAction.Click

Me.lbResult.Items.Clear()

Dim iCountries As List(Of String)
iCountries = New List(Of String)

iCountries.Add("Arab")
iCountries.Add("China")
iCountries.Add("Denmark")
iCountries.Add("India")
iCountries.Add("Malaysia")
iCountries.Add("Taiwan")
iCountries.Add("America")
iCountries.Add("Canada")
iCountries.Add("Singapore")
iCountries.Add(2)
iCountries.ForEach(AddressOf SelectCountries)
'Action = Take one parameter of any type without return anything
End Sub

 

Public Sub SelectCountries(ByVal c As String)

Dim strcountries As String = Nothing

strcountries = c

Me.lbResult.Items.Add(strcountries) ' add item at a time

End Sub

 

Converter (Converter takes 1 input and return 1 output type)


Private Sub btnConverter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConverter.Click

Dim dResult As String = Nothing
Dim numbers As New List(Of Integer)

For i As Integer = 1 To 9
numbers.Add(i)
Next

Dim converter As New Converter(Of Integer, String)(AddressOf ConvertMethod)
Dim numstring As List(Of String) = numbers.ConvertAll(Of String)(converter)

For r As Integer = 0 To numstring.Count - 1
dResult += numstring(r) & " "
Next
Me.lblPath.Text = dResult
'Converter takes 1 input and return 1 output type

End Sub

 

Private Shared Function ConvertMethod(ByVal i As Integer) As String
Dim convertedset As String()
convertedset = New String() {"One ", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
Return convertedset(i - 1)

End Function

 

 

 

 

 

@Copy right of Soon Lim 2006. All Right Reserved