Use of Switch Statement and Ternarry operator with Statement Lambda using VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 152
The below code will show the way to use Switch Statement and Ternary operator with Statement Lambda using VB.net

Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim personData = New List(Of PersonMaster)()

'populating some data
Enumerable.Range(1, 10).ToList().ForEach(Function(i) personData.Add(New PersonMaster() With { _
Key .PersonID = i, _
Key .PersonName = String.Concat("Person ", i), _
Key .Age = 14 + i _
}))

'Use of Statement Lambda
Dim eligiblePersonData = New List(Of EligiblePerson)()

personData.ToList().ForEach(Function(i)

Select Case If(i.Age < 18, 0, 1)
' note the way the ternarry operator is used inside the switch statement as an expression
Case 0
Console.WriteLine("Mr. {0} is not eligible to case vote as his age is {1} which is under 18", i.PersonName, i.Age)
Exit Select
Case 1
eligiblePersonData.Add(New EligiblePerson() With { _
Key .PersonID = i.PersonID, _
Key .PersonName = i.PersonName, _
Key .Age = i.Age _
})
Exit Select
End Select

End Function)

Console.ReadKey()
End Sub
End Class

'The main person model
Public Class PersonMaster
Public Property PersonID() As Integer
Get
Return m_PersonID
End Get
Set
m_PersonID = Value
End Set
End Property
Private m_PersonID As Integer
Public Property PersonName() As String
Get
Return m_PersonName
End Get
Set
m_PersonName = Value
End Set
End Property
Private m_PersonName As String
Public Property Age() As Integer
Get
Return m_Age
End Get
Set
m_Age = Value
End Set
End Property
Private m_Age As Integer

End Class

'Eligible Person Model
Public Class EligiblePerson
Public Property PersonID() As Integer
Get
Return m_PersonID
End Get
Set
m_PersonID = Value
End Set
End Property
Private m_PersonID As Integer
Public Property PersonName() As String
Get
Return m_PersonName
End Get
Set
m_PersonName = Value
End Set
End Property
Private m_PersonName As String
Public Property Age() As Integer
Get
Return m_Age
End Get
Set
m_Age = Value
End Set
End Property
Private m_Age As Integer
End Class
End Namespace

Comments or Responses

Login to post response