|
|
|||
| H o m e | |||
|
|
G e t A c q u a i n t e d W i t h Q u e u e C l a s s By Michael McIntyre
This article introduces the Queue class and provides example code that demonstrates how to use a Queue object. The Queue class is located within the .NET Framework’s System.Collections namespace.
A Queue object is a first-in, first-out collection of objects. Queues are useful for storing things in the order they were received for sequential processing. Objects stored in a Queue collection are inserted at one end and removed from the other.
The following code snippets demonstrate how to use a Queue object to keep track of each player’s turn in a game application.
Example One – Declare and instantiate a Queue object.
' Declare a variable of type Queue named _PlayerQueue. ' Use a Queue constructor to create a new Queue object. ' Assign the address (reference) of the new object ' to the _PlayerQueue variable. Private _PlayerQueue As New Queue()
Example Two – A method named FillPlayerQueue is used to fill the _PlayerQueue object with player names
Private Sub FillPlayerQueue() ' The Queue Enqueue method is used to add objects to the front of a Queue. ' Add four players to _PlayerQueue. _PlayerQueue.Enqueue("Mike") _PlayerQueue.Enqueue("Mary") _PlayerQueue.Enqueue("Bill") _PlayerQueue.Enqueue("Tony")
' By default a Queue is given capacity to hold 32 objects. ' The Queue TrimToSize method sets the capacity to the ' actual number of elements in the Queue. ' Trim the size of _PlayerQue to four players. _PlayerQueue.TrimToSize() End Sub
Example Three - A function named GetNextPlayer pulls a player from the front of the _PlayerQueue and then adds same player back to the _PlayerQueue – at the end of the Queue - so the player’s turn will come up again after the other players have had their turn.
Private Function GetNextPlayer() As String ' Declare a variable of type String named player. Dim player As String
' The Queue Dequeue method removes and returns the object ' at the beginning of the Queue. ' Get the next player from the _PlayerQueue. player = _PlayerQueue.Dequeue.ToString
' Trim _PlayerQueue to size. _PlayerQueue.TrimToSize()
' The Queue Enqueue adds an object to the end of the Queue. ' Add the player back to the Queue, at the end of the Queue ' so the player's turn will come up again after the other ' players have their turns. _PlayerQueue.Enqueue(player)
' Return the player. Return player End Function
Example Four - A function named ReportPlayerQueueStatus uses Queue methods to report the next player, current player order, and the number of players.
Private Function ReportPlayerQueueStatus() As String ' Declare a variable of type String named report. Dim report As String
' Build the report. report += "Player Queue Status Report" & vbCrLf report += "----------------------------" & vbCrLf ' The Queue Peek method returns the object at the front of the Queue ' without removing it from the Queue. report += "Next player will be " & _PlayerQueue.Peek.ToString & "." & vbCrLf ' The Queue Count method returns the number of objects in a Queue. report += "Number of players is " & _PlayerQueue.Count.ToString & vbCrLf & vbCrLf ' Add Title for player list. report += "Players" & vbCrLf report += "----------------------------" ' The Queue CopyTo method is used to copy the objects ' in a Queue to an array. ' Copy _PlayerQueue objects to an array. Dim players(_PlayerQueue.Count) As String _PlayerQueue.CopyTo(players, 0) ' List the players in the Queue. Dim player As String For Each player In players report += player & vbCrLf Next
Return report End Function
Learn more about the Queue class by clicking here.
|
|
|
|
Copyright © 2001-2004 aZ Software Developers. All rights reserved. |
|||