Queue là một thư viện nằm trong “System.Co" />
RaoVat24h
Excel Office

Bài 16. Queue

Advertisement

Queue là một thư viện nằm trong “System.Collections.” của .NET Framework. cùng.
Đây là cấu trúc dữ liệu dựng theo hàng để thực hiện tính chất FIFO (First In – First Out / Vào trước – Ra trước).
Cho phép lưu trữ dữ liệu (items) có kích cỡ lớn, rất hữu ích trong các tình huống muốn lưu trữ các Items theo thứ tự chỉ định.
Yêu cầu: Hệ thống phải cài đặt .NET Framework

1. Khai báo Queue

1.1. Kiểu khai báo sớm
(Không có Tooltip khi gọi Queue, phải thiết lập trong Tools/References)
– Trong cửa sổ VBA, Tools menu, References.
– Tìm và check vào mục “mscorlib.dll” trong cửa sổ References – VBAProject.
Khai báo trong code:

Dim oQueue As New Queue


1.2. Kiểu khai báo muộn
(Không có Tooltip khi gọi Queue, không cần thiết lập trong Tools/References).
Khai báo trong code:

Dim oQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")


2. Các phương thức, thuộc tính


2.1. Count Property

oQueue.Count


Trả về số Items có trong Queue.
Ví dụ:

Sub CountProperty()
    Dim oQueue As Object
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Count'
    oQueue.Enqueue 5
    oQueue.Enqueue "TextA"
    MsgBox oQueue.Count   '2'
End Sub


2.2. Enqueue

oQueue.Enqueue Item


Thêm một Item vào vị trí cuối cùng (end) của Queue.
Item nhận kiểu dữ liệu bất kỳ (kiểu số hoặc chuỗi), giá trị đơn hoặc một mảng (array).
Ví dụ:

Sub EnqueueMethod()
    Dim oQueue As Object
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Enqueue Item'
    oQueue.Enqueue 5
    oQueue.Enqueue "TextA"
    oQueue.Enqueue ""
    oQueue.Enqueue Array(20, 40)
End Sub


2.3. Peek
oQueue.Peek
Trả về Item đầu tiên của Queue và không xóa Item đó.
Ví dụ:

Sub PeekMethod()
    Dim oQueue As Object, i As Long
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Peek'
    For i = 1 To 10
        oQueue.Enqueue "Value-" & i
    Next i
    MsgBox oQueue.Peek  'Value-1'
End Sub


2.4. Dequeue

oQueue.Dequeue


Xóa và trả về Item đầu tiên của Queue.
Ví dụ:

Sub DequeueMethod()
    Dim oQueue As Object, i As Long, sValue As String
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Dequeue'
    For i = 1 To 10
        oQueue.Enqueue "Value-" & i
    Next i
    sValue = oQueue.Dequeue
    MsgBox sValue       'Value-1'
    MsgBox oQueue.Count '9'
End Sub


2.5. Contains

oQueue.Contains


Kiểm tra sự tồn tại của một Item trong Queue. Trả về True nếu Item đó tồn tại, ngược lại trả về False.
Ví dụ:

Sub ContainsMethod()
    Dim oQueue As Object
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Contains'
    oQueue.Enqueue 5
    oQueue.Enqueue "TextA"
    MsgBox oQueue.Contains(5)       'True'
    MsgBox oQueue.Contains("TextA") 'True'
    MsgBox oQueue.Contains("TextB") 'False
End Sub


2.6. ToArray

oQueue.ToArray


Sao chép các Item trong Queue vào một mảng (Array). Mảng trả về là mảng một chiều, chỉ số cận dưới của mảng luôn băng 0, cho dù thiết lập Option Base 1.
Ví dụ:

Sub ToArrayMethod()
    Dim oQueue As Object, i As Long, arr()
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.ToArray     Mang 1 chieu, khong phu thuoc Option Base 1'
    For i = 1 To 10
        oQueue.Enqueue "Value-" & i
    Next i
    arr = oQueue.ToArray
    MsgBox arr(0)   'Value-1'
End Sub


2.7. ToString

oQueue.ToString


Trả về tên đối tượng hiện hành, tức là “System.Collections.Queue”.
Ví dụ:

Sub ToStringMethod()
    Dim oQueue As Object, sName As String
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.ToString'
    sName = oQueue.ToString
    MsgBox sName    'System.Collections.Queue'
End Sub


2.8. Clear

oQueue.Clear


Xóa tất cả các Items có trong Queue.
Ví dụ:

Sub ClearMethod()
    Dim oQueue As Object, i As Long
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Clear'
    For i = 1 To 10
        oQueue.Enqueue i
    Next i
    oQueue.Clear
    MsgBox oQueue.Count '0'
End Sub


2.9. Clone

oQueue.Clone


Sao chép toàn bộ Queue đã dựng sang một Queue mới.
Ví dụ:

Sub CloneMethod()
    Dim oQueue As Object, newQueue As Object
    Set oQueue = CreateObject("System.Collections.Queue")
    'oQueue.Clone'
    oQueue.Enqueue 20
    Set newQueue = oQueue.Clone
    MsgBox newQueue.Peek    '20'
End Sub


3. Ứng dụng
– Lọc loại trùng
– …

3.1. Hàm lọc loại trùng trong một cột

'// Loc loai trung mot cot'
Public Function UniqueColumnQueue(ByVal Rng As Range) As Variant
    If Rng.Count = 1 Then UniqueColumnQueue = Rng.Value: Exit Function
    Dim oQueue As Object, i As Long, j As Long, arr(), Result(), sKey As Variant
    Set oQueue = CreateObject("System.Collections.Queue")
    arr = Rng.Value
    For i = LBound(arr, 1) To UBound(arr, 1)
        sKey = arr(i, 1)
        If sKey <> "" Then
            If oQueue.Contains(sKey) = False Then
                oQueue.Enqueue sKey
                j = j + 1
                ReDim Preserve Result(1 To j)
                Result(j) = sKey
            End If
        End If
    Next i
    UniqueColumnQueue = Result
End Function
Rate this post

DienDan.Edu.Vn

DienDan.Edu.Vn Cám ơn bạn đã quan tâm và rất vui vì bài viết đã đem lại thông tin hữu ích cho bạn.
DienDan.Edu.Vn! là một website với tiêu chí chia sẻ thông tin,... Bạn có thể nhận xét, bổ sung hay yêu cầu hướng dẫn liên quan đến bài viết. Vậy nên đề nghị các bạn cũng không quảng cáo trong comment này ngoại trừ trong chính phần tên của bạn.
Cám ơn.

Đăng bình luận

(+84) (901) 369.468