Posted by: Sourav | September 14, 2016

Dictionary Example 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 = “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

Console.ReadLine()

End Sub

End Module
Public Class customer
Private 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: