Posted by: Sourav | September 14, 2016

More dictionary example,array and list of objects to dictionary conversion,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 = “Sougata”, .salarymanipulate = 5000}
Dim customer3 As customer = New customer() With {.idmanipulate = 3, .namemanipulate = “Subrata”, .salarymanipulate = 7000}
Dim dictionarycustomers As Dictionary(Of Integer, customer) = New Dictionary(Of Integer, customer)
dictionarycustomers.Add(customer1.idmanipulate, customer1)
dictionarycustomers.Add(customer2.idmanipulate, customer2)
dictionarycustomers.Add(customer3.idmanipulate, customer3)
‘ok so let’s retrieve single customer using id
Dim samplecustomer As customer
If dictionarycustomers.ContainsKey(1) Then
samplecustomer = dictionarycustomers(1)

Console.WriteLine(“ID= {0}, Name= {1},Salary= {2}”, samplecustomer.idmanipulate, samplecustomer.namemanipulate, samplecustomer.salarymanipulate)
End If
‘ ok so let’s loop through each item in the dictionary
For Each customerkeyvaluepair As KeyValuePair(Of Integer, customer) In dictionarycustomers
Console.WriteLine(“Key= {0}”, customerkeyvaluepair.Key)

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

Next
‘Now instead of keyvaluepair type we can use implicit type such as var in Csharp
‘to do this we have to omit the type in vb
http://stackoverflow.com/questions/2478552/vb-net-equivalent-to-c-sharp-var-keyword

‘For Each customerkeyvaluepair In dictionarycustomers
‘    Console.WriteLine(“ID= {0}”, customerkeyvaluepair.Key)
‘    samplecustomer = customerkeyvaluepair.Value
‘    Console.WriteLine(“ID= {0}, Name= {1},Salary= {2}”, samplecustomer.idmanipulate, samplecustomer.namemanipulate, samplecustomer.salarymanipulate)
‘    Console.WriteLine(“****************************************”)

‘Next
‘Ok so let’s try to print all the keys
Console.WriteLine()
Console.WriteLine()
For Each key As Integer In dictionarycustomers.Keys
Console.WriteLine(“Key= {0}”, key)

Console.WriteLine(“****************************************”)

Next

‘Ok so let’s try more dictionary feature such as try get value,if I want to get a value based on
‘ a key and if the key is not present and if I want to handle all the exceptions raised
‘in that case all by themselves automatically I should use this

Dim custsamp As customer
dictionarycustomers.TryGetValue(4, custsamp)
If Not IsNothing(custsamp) Then
Console.WriteLine(“ID= {0}, Name= {1},Salary= {2}”, custsamp.idmanipulate, custsamp.namemanipulate, custsamp.salarymanipulate)
Else
Console.WriteLine(“The key is not found “)

End If
‘Ok let’s count the total number of items in the dictionary
Console.WriteLine(“The total number of elements the dictionary is {0} “, dictionarycustomers.Count)
‘ok let’s count the total number of items in the dictionary based on a criteria
‘  Console.WriteLine(“The total number of elements the dictionary is {0} “, dictionarycustomers.Count(Function(x) x.Value.Salary > 5000))

‘Ok let’s remove an item
dictionarycustomers.Remove(110)
‘remove all items from the dictionary
dictionarycustomers.Clear()
‘let’s convert an array to a dictionary
Dim customers As customer() = New customer(2) {}
customers(0) = customer1
customers(1) = customer2
customers(2) = customer3

‘Dim dict As Dictionary(Of Integer, customer) = New Dictionary(Of Integer, customer)
‘ dict = customers.ToDictionary(Function(customer) customer.idmanipulate, Function(customer) customer)

Dim dict As Dictionary(Of Integer, customer) = customers.ToDictionary(Function(customer) customer.id, Function(customer) customer)
‘ dict.Add(customers(0).idmanipulate, customer1)

Console.WriteLine(“This is after conversion from array to dictionary”)
Console.WriteLine()

For Each customerkeyvaluepair As KeyValuePair(Of Integer, customer) In dict
Console.WriteLine(“Key= {0}”, customerkeyvaluepair.Key)

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

Next
‘Alright so let’s try doing the same thing with list,we are going to convert this to dictionary
Dim customers2 As List(Of customer) = New List(Of customer)
customers2.Add(customer1)
customers2.Add(customer2)
customers2.Add(customer3)

Dim dict2 As Dictionary(Of Integer, customer) = customers.ToDictionary(Function(customer) customer.id, Function(customer) customer)
‘ dict.Add(customers(0).idmanipulate, customer1)

Console.WriteLine(“This is after conversion from list to dictionary”)
Console.WriteLine()

For Each customerkeyvaluepair As KeyValuePair(Of Integer, customer) In dict2
Console.WriteLine(“Key= {0}”, customerkeyvaluepair.Key)

samplecustomer = 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

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: