Posted by: Sourav | September 16, 2016

More List Collection Class in VB,VB teacher Sourav,Kolkata 09748184075


Option Strict On
Option Explicit On
Imports System
Imports System.Collections.Generic
Module Module1

Sub Main()
Dim customer1 As customer = New customer() With {.idmanipulate = 1, .namemanipulate = “Sourav”, .salarymanipulate = 3000}
Dim customer2 As customer = New customer() With {.idmanipulate = 2, .namemanipulate = “kunal”, .salarymanipulate = 5000}

Dim customer3 As customer = New customer() With {.idmanipulate = 3, .namemanipulate = “Subrata”, .salarymanipulate = 7000}
Dim listCustomers As New List(Of customer)(2)
listCustomers.Add(customer1)
listCustomers.Add(customer2)
listCustomers.Add(customer3)
‘Dim c As customer = listCustomers(0)
‘Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, c.idmanipulate, c.namemanipulate, c.salarymanipulate)
‘For Each c As customer In listCustomers
‘    Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, c.idmanipulate, c.namemanipulate, c.salarymanipulate)
‘Next
Dim i As Integer
For i = 0 To listCustomers.Count – 1
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, listCustomers(i).idmanipulate, listCustomers(i).namemanipulate, listCustomers(i).salarymanipulate)

Next

Dim sc As savingscustomer = New savingscustomer
‘listCustomers.Add(sc)
‘ listCustomers.Add(100)
‘list generic clollection class is strongly typed ,which means now I can not insert
‘anything other than customer type in the list
‘Now let’s try to insert an object at a particular location
listCustomers.Insert(0, customer3)
For i = 0 To listCustomers.Count – 1
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, listCustomers(i).idmanipulate, listCustomers(i).namemanipulate, listCustomers(i).salarymanipulate)

Next
‘ok let’s try to find the index of a particular object
Console.WriteLine(“the index of customer3 object is {0}”, listCustomers.IndexOf(customer3))
Console.WriteLine(“the second index of customer3 object is {0}”, listCustomers.IndexOf(customer3, 1))
Console.WriteLine(“the second index of customer3 object is {0}”, listCustomers.IndexOf(customer3, 1, 3))
‘ok let’s do some more
‘let’s check if this list contains an object
Console.WriteLine(“The fact that the object customer1 is present in the list is {0}”, listCustomers.Contains(customer1))
‘ok so let’s try exist which support lambda expression
If listCustomers.Exists(Function(x) x.namemanipulate.StartsWith(“k”)) Then
Console.WriteLine(“List contains customer whose name starts with k”)
Else
Console.WriteLine(“List does not contain a customer whose name starts with k”)
End If

‘ok let’s use find method which will find the first occurance in a list based
‘on a condition
Dim c As customer = listCustomers.Find(Function(x) x.salarymanipulate > 7000)
If Not IsNothing(c) Then
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, c.idmanipulate, c.namemanipulate, c.salarymanipulate)
End If
‘let’s try findlast
Dim d As customer = listCustomers.FindLast(Function(x) x.salarymanipulate > 3000)
If Not IsNothing(d) Then
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, d.idmanipulate, d.namemanipulate, d.salarymanipulate)
End If

‘let’s try findall
Console.WriteLine(“Findall example”)

Dim filteredcustomers As List(Of customer) = listCustomers.FindAll(Function(x) x.salarymanipulate > 5000)
For Each e As customer In filteredcustomers
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, e.idmanipulate, e.namemanipulate, e.salarymanipulate)
Next
‘let’s try findindex
Dim index As Integer = listCustomers.FindIndex(Function(x) x.salarymanipulate <= 5000)
Console.WriteLine(“index = {0}”, index)
index = listCustomers.FindIndex(0, Function(x) x.salarymanipulate <= 5000)
Console.WriteLine(“index = {0}”, index)
‘let’s try lastindex
index = listCustomers.FindLastIndex(Function(x) x.salarymanipulate <= 7000)
Console.WriteLine(“index = {0}”, index)
‘ok let’s convert an array to a list
Dim customers As customer() = New customer(2) {}
customers(0) = customer1
customers(1) = customer2
customers(2) = customer3
Dim listCustomersconverted As List(Of customer) = customers.ToList
For Each f As customer In listCustomersconverted
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, f.idmanipulate, f.namemanipulate, f.salarymanipulate)
Next

‘ok let’s convert a list to array
Dim listtoarraycustomers As List(Of customer) = New List(Of customer)

listtoarraycustomers.Add(customer1)
listtoarraycustomers.Add(customer2)
listtoarraycustomers.Add(customer3)
Dim customersarr As customer() = New customer(2) {}
customersarr = listtoarraycustomers.ToArray
Console.WriteLine(“this is from converted array”)

For Each g As customer In customersarr
Console.WriteLine(“ID= {0}, Name={1} ,Salary={2}”, g.idmanipulate, g.namemanipulate, g.salarymanipulate)
Next
‘ok let’s convert a list to dictionary
Dim dict As Dictionary(Of Integer, customer) = listtoarraycustomers.ToDictionary(Function(customer) customer.id, Function(customer) customer)
For Each customerkeyvaluepair As KeyValuePair(Of Integer, customer) In dict
Console.WriteLine(“Key= {0}”, customerkeyvaluepair.Key)

Dim samplecustomer As customer = customerkeyvaluepair.Value
Console.WriteLine(“ID= {0}, Name= {1},Salary= {2}”, samplecustomer.idmanipulate, samplecustomer.namemanipulate, samplecustomer.salarymanipulate)
Console.WriteLine(“****************************************”)

Next
Console.ReadLine()

End Sub

End Module
Public Class customer
Public id As Integer
Private name As String
Private salary As Integer
Public Property idmanipulate As Integer
Set(ByVal id As Integer)
Me.id = id
End Set
Get
Return Me.id

End Get
End Property
Public Property namemanipulate As String
Set(ByVal name As String)
Me.name = name
End Set
Get
Return Me.name

End Get
End Property
Public Property salarymanipulate As Integer
Set(ByVal salary As Integer)
Me.salary = salary
End Set
Get
Return Me.salary

End Get
End Property

End Class
Public Class savingscustomer
Inherits customer

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: