Share Hướng Dẫn Sử Dụng Dictionary Trong VB.NET: Lưu Trữ Dữ Liệu Đối Tượng

duongariana

New member
#vb.net #dicesary #DatSource #hashtable #key-valuePair ## Hướng dẫn sử dụng từ điển trong vb.net

Từ điển là một cấu trúc dữ liệu lưu trữ dữ liệu trong các cặp giá trị khóa.Các khóa là duy nhất và các giá trị có thể thuộc bất kỳ loại nào.Bạn có thể sử dụng từ điển để lưu trữ dữ liệu theo cách vừa hiệu quả vừa có tổ chức.

Để tạo một từ điển trong vb.net, bạn sử dụng cú pháp sau:

`` `
Dim mydictionary là từ điển mới (của chuỗi, số nguyên)
`` `

Tham số đầu tiên của hàm tạo từ điển là loại khóa và tham số thứ hai là loại giá trị.Trong ví dụ này, các khóa thuộc loại loại và các giá trị là số nguyên loại.

Bạn có thể thêm các mục vào từ điển bằng phương thức Thêm.Phương thức Thêm lấy hai tham số: khóa và giá trị.

`` `
mydictionary.add ("Apple", 1)
mydictionary.add ("màu cam", 2)
`` `

Bạn có thể lấy các mục từ một từ điển bằng thuộc tính mục.Thuộc tính mục lấy khóa làm tham số và trả về giá trị tương ứng.

`` `
Dim AppleCount As Integer = MyDictionary ("Apple")
`` `

Bạn cũng có thể lặp lại thông qua các mục trong từ điển bằng cách sử dụng cho mỗi vòng lặp.

`` `
Đối với mỗi mục trong MyDictionary
Console.WriteLine (item.key & ":" & item.Value)
Kế tiếp
`` `

Sau đây là một ví dụ về một chương trình sử dụng từ điển để lưu trữ tên và độ tuổi của mọi người.

`` `
Chương trình mô -đun

Sub main ()
'Tạo một từ điển để lưu trữ tên và lứa tuổi của mọi người.
Dim People là Từ điển mới (của Chuỗi, Số nguyên)

'Thêm một số mục vào từ điển.
People.Add ("John", 20)
People.Add ("Mary", 21)
People.Add ("Bill", 22)

'Lặp lại thông qua các mục trong từ điển.
Đối với mỗi mặt hàng trong mọi người
Console.WriteLine (item.key & ":" & item.Value)
Kế tiếp

'Nhận tuổi của John.
Dim Johnage như số nguyên = People ("John")

'In tuổi của John.
Console.WriteLine ("John là" & Johnage & "tuổi.")

Kết thúc phụ

Mô -đun kết thúc
`` `

## hashtags

* #vb.net
* #Từ điển
* #Cấu trúc dữ liệu
* #hashtable
* #key-ValuePair
=======================================
#vb.net #dictionary #datastructure #hashtable #key-ValuePair ## Instructions for using Dictionary in VB.NET

A Dictionary is a data structure that stores data in key-value pairs. The keys are unique, and the values can be of any type. You can use a Dictionary to store data in a way that is both efficient and organized.

To create a Dictionary in VB.NET, you use the following syntax:

```
Dim myDictionary As New Dictionary(Of String, Integer)
```

The first parameter of the Dictionary constructor is the type of the keys, and the second parameter is the type of the values. In this example, the keys are of type String, and the values are of type Integer.

You can add items to a Dictionary using the Add method. The Add method takes two parameters: the key and the value.

```
myDictionary.Add("Apple", 1)
myDictionary.Add("Orange", 2)
```

You can retrieve items from a Dictionary using the Item property. The Item property takes the key as a parameter, and returns the corresponding value.

```
Dim appleCount As Integer = myDictionary("Apple")
```

You can also iterate through the items in a Dictionary using a For Each loop.

```
For Each item In myDictionary
Console.WriteLine(item.Key & " : " & item.Value)
Next
```

The following is an example of a program that uses a Dictionary to store the names and ages of people.

```
Module Program

Sub Main()
' Create a Dictionary to store the names and ages of people.
Dim people As New Dictionary(Of String, Integer)

' Add some items to the Dictionary.
people.Add("John", 20)
people.Add("Mary", 21)
people.Add("Bill", 22)

' Iterate through the items in the Dictionary.
For Each item In people
Console.WriteLine(item.Key & " : " & item.Value)
Next

' Get the age of John.
Dim johnAge As Integer = people("John")

' Print the age of John.
Console.WriteLine("John is " & johnAge & " years old.")

End Sub

End Module
```

## Hashtags

* #vb.net
* #dictionary
* #datastructure
* #hashtable
* #key-ValuePair
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top