Posted by: Sourav | October 30, 2016

Func Delegate in VB.Net,VB Teacher Sourav,Kolkata 09748184075


Imports System.Collections.Generic
Imports System.Linq

Module Module1
Public Sub Main()
Dim listEmployees As New List(Of Employee)() From { _
New Employee() With { _
.ID = 101, _
.Name = “Mark” _
}, _
New Employee() With { _
.ID = 102, _
.Name = “John” _
}, _
New Employee() With { _
.ID = 103, _
.Name = “Mary” _
} _
}

‘ Create a Func delegate
‘Dim selector As Func(Of Employee, String) = Function(employee) “Name = ” + employee.Name
‘ Pass the delegate to the Select() LINQ function
Dim names As IEnumerable(Of String) = listEmployees.[Select](Function(employee) “Name = ” + employee.Name)
Dim funcdelegatetoadd As Func(Of Integer, Integer, String) = Function(
firstnum, secondnum) “Sum= ” + (firstnum + secondnum).ToString()

Dim result As String = funcdelegatetoadd(10, 20)
Console.WriteLine(“The result of the sum is ” + result)

‘ The above output can be achieved using
‘ lambda expression as shown below
‘ IEnumerable<string> names =
‘ listEmployees.Select(employee => “Name = ” + employee.Name);

For Each name As String In names
Console.WriteLine(name)
Next

Console.ReadLine()

End Sub

End Module

‘=======================================================
‘Service provided by Telerik (www.telerik.com)
‘Conversion powered by NRefactory.
‘Twitter: @telerik
‘Facebook: facebook.com/telerik
‘=======================================================

Public Class Employee
Public Property ID() As Integer
Get
Return m_ID
End Get
Set(ByVal value As Integer)
m_ID = value
End Set
End Property
Private m_ID As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: