Trong bài viết này mình sẽ giới thiệu một số kỹ thuật thao tác với Dictionary Collection trong VB.Net:
1. Kỹ thuật thêm Value có cùng Key vào Dictionary
1. Kỹ thuật thêm Value có cùng Key vào Dictionary
Module MinhHoangBlog Sub Main() ' Declare List data Dim listData As New List(Of String) From { {"first:1"}, {"second:2"}, {"third:1"}, {"first:2"}, {"first:3"}, {"third:2"} } ' Declare mapData: Key is String, Value is List<int> Dim mapData As New Dictionary(Of String, List(Of Integer)) ' Get data of mapData For Each item In listData ' Split item by colon Dim parts() As String = item.Split( ":" ) If Not mapData.ContainsKey(parts(0)) Then mapData.Add(parts(0), New List(Of Integer)) End If mapData(parts(0)).Add(parts(1)) Next ' Print data of mapData For Each kvp As KeyValuePair(Of String, List(Of Integer)) In mapData ' Declare Keyis String Dim key = kvp.Key ' Declare Value is List<int> Dim listVal As List(Of Integer) = kvp.Value Console.WriteLine("・Key: {0}", key) For Each item In listVal Console.WriteLine(vbTab & "Value: {0}", item) Next Next Console.ReadKey() End Sub End Module

Kết quả chương trình
2. Một số kỹ thuật thao tác trên Dictionary
2. Một số kỹ thuật thao tác trên Dictionary
Trong code example này có sử dụng đến kiến thức của Extension method trong VB.Net.
Imports System.Runtime.CompilerServices Module MinhHoangBlog Sub Main() ' Declare and initialize the value of a dictionary Dim mapStudents As New Dictionary(Of Integer, String) From { {123, "Tom"}, {444, "Jerry"}, {555, "Peter"} } ' Add new data to dictionary mapStudents.Add( 666, "Marry" ) ' Print Value based on Key Console.WriteLine(mapStudents(444)) 'Output: Jerry ' New Line Console.WriteLine() ' Print Key and Value For Each stu In mapStudents Console.WriteLine(stu.Key & " = " & stu.Value) Next ' Get Key-Value using LINQ Dim result1 As KeyValuePair(Of Integer, String) = mapStudents.Single(Function(item) item.Key = 444) ' Get Key-Value using extension method Dim result2 As KeyValuePair(Of Integer, String) = mapStudents.GetValueFromKey(444) ' Update value of map data based on key Dim searchKey As Integer = result2.Key If mapStudents.ContainsKey(searchKey) Then ' 値更新 mapStudents(searchKey) = "minhhn.com" End If ' New Line Console.WriteLine() ' Iterate in Dictionary For Each kvp As KeyValuePair(Of Integer, String) In mapStudents Dim key As Integer = kvp.Key Dim val As String = kvp.Value ' Do whatever you want with key, val: Console.WriteLine("{0} = {1}", key, val) Next ' Loop by key For Each iKey As Integer In mapStudents.Keys Dim val As String = mapStudents(iKey) '... '.....Write logic codes Here.... Next Console.ReadKey() End Sub ' Extension method ''' <summary>データマップのキーを基にして値を取得する</summary> ''' <param name="mapData">データマップ</param> ''' <param name="key">キー</param> ''' <returns>値</returns> <Extension()> Public Function GetValueFromKey(Of TKey, TValue)(mapData As Dictionary(Of TKey, TValue), key As TKey) As KeyValuePair(Of TKey, TValue) Dim value As TValue If mapData.TryGetValue(key, value) Then Return New KeyValuePair(Of TKey, TValue)(key, value) End If Return Nothing End Function End Module

Kết quả chương trình
Bạn ơi, bạn có khóa VB.Net hay C# cho lập trình AutoCad không bạn?
Hi, không bạn ơi. Có khó khăn gì bạn cứ hỏi mình sẽ support.
Mình thấy VB.net dể hiểu hơn C#, nhưng mình lại không có sách học VB.net, tìm trên mạng thì toàn tiếng anh không, nhiều người bỏ nên học C#, vì nó lập trình hướng đối tượng, còn VB.net thì không, nên hiện tại mình đang chập chững viết các class trong C# với các hàm đơn giản để phục vụ cho công việc trong phần mềm Autocad sau đó build ra file DLL, chỉ việc Netload vào Autocad là xài được, và mình muốn share cho các đồng nghiệp dùng, nhưng không phải ai cũng biết cách dùng lệnh Netload,… Read more »