TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Queue Examples

Use the Queue generic collection to store elements in a first-in-first-out way.
Queue. A Queue is a generic collection in VB.NET. It implements a FIFO algorithm. With Queue, you can keep a sliding cache of elements, with the one added first always being the first to be removed.Stack

First-In-First-Out: The element that is added first (with Enqueue) is also the first one that is removed (with Dequeue).

Enqueue. This program demonstrates two important concepts of the Queue type. First, it shows the Enqueue method, which you use to add an element to the Queue. Next, it loops over the elements in the Queue with the For-Each looping construct.

Note: For Each is implemented in a special way so that it shows all the internal elements in the Queue.

For Each, For
VB.NET program that uses Queue generic type Module Module1 Sub Main() ' Add integers to Queue. Dim q As Queue(Of Integer) = New Queue(Of Integer)() q.Enqueue(5) q.Enqueue(10) q.Enqueue(15) q.Enqueue(20) ' Loop over the Queue. For Each element As Integer In q Console.WriteLine(element) Next End Sub End Module Output 5 10 15 20
Enum Queue. In this example, we explore how the Queue can be used to store help requests in a system. As users request help for a program, the requests can be added to the Queue with Enqueue.

Enum: Requests (represented by the RequestType enum) can be read with Dequeue after testing them with Peek.

Enum

Important: With a Queue, the oldest (first added) help requests will be the first to be handled.

Stack: With a Stack, the newest help requests would be the first to be handled. You would always provide help to people who most recently requested it.

VB.NET program that uses Enum Queue Module Module1 Enum RequestType As Integer MouseProblem TextProblem ScreenProblem ModemProblem End Enum Sub Main() Dim help As Queue(Of RequestType) = New Queue(Of RequestType)() ' Add some problems to the queue. help.Enqueue(RequestType.TextProblem) help.Enqueue(RequestType.ScreenProblem) ' If first problem is not a mouse problem, handle it. If help.Count > 0 Then If Not help.Peek = RequestType.MouseProblem Then ' This removes TextProblem. help.Dequeue() End If End If ' Add another problem. help.Enqueue(RequestType.ModemProblem) ' See all problems. For Each element As RequestType In help Console.WriteLine(element.ToString()) Next End Sub End Module Output ScreenProblem ModemProblem
CopyTo. How can you copy a Queue to an array? Some other functions in your program may demand an array argument or you may want to simply store the elements in an array field. To copy to an array, first allocate an array with the array syntax.

Next: Call CopyTo and pass the reference variable to that array as the first argument.

VB.NET program that uses CopyTo Module Module1 Sub Main() ' New queue. Dim q As Queue(Of Integer) = New Queue(Of Integer)() q.Enqueue(5) q.Enqueue(10) q.Enqueue(20) ' Create new array of required length. Dim arr(q.Count - 1) As Integer ' CopyTo. q.CopyTo(arr, 0) ' Display elements. For Each element In arr Console.WriteLine(element) Next End Sub End Module Output 5 10 20
Summary. In these examples, we explored some characteristics of the Queue collection in the VB.NET language. With Queue, we gain a FIFO collection implementation. This can be used to implement higher-level concepts such as a help request system.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf