Exploring the System.IO.Path Namespace - Part IIIntroduction To VB.NET Generic Stacks

Introduction to VB.NET Generic Queues

August 21st, 2007

Elf with a Queue of GiftsQueues are one of the common classic data structures found in computer science. A queue is simply a First-In-First-Out (aka FIFO) buffer where new items are added to the end of the list and the item at the front of the list is processed first. Implementing one in VB hasn’t always been easy due to the lack of pointers. While there were some ways to work around this, the programming could get messy and difficult to understand. However, the Queue collection type was introduced in the original .NET Framework and now, in the .NET Framework 2.0, we now have the generic Queue class to make it a breeze to implement.

As with other .NET Generic Collection classes, the queue is a type-safe collection. This means that you must define the type of object that is to be queued and only objects of that type can be placed in the queue. Here are a few examples:

Dim NameQueue As Queue(Of String)

Dim MailQueue As Queue(Of Net.Mail.MailMessage)

Dim OrderQueue As Queue(Of OrderDetailItems)

The basic operations of a queue are enqueue and dequeue. Enqueue places a new item at the end of the queue while Dequeue removes the first item in the queue from the list and returns it to the caller. The queue automatically expands to accomidate new members. Remember that if the queue is empty that the Dequeue function will return an InvalidOperationException. Here are some simple examples:

NameQueue.Enqueue(CurrentName)

Do While MailQueue.Count > 0
    SendMail(MailQueue.Dequeue)
Loop

If you wanted to view the next item in the queue without removing it, you can use the Peek function as seen in this example:

If IsSignedUp(MailQueue.Peek.To) Then
    SendMarketingEmail(MailQueue.Dequeue)
Else
    SendSignUpEmail(MailQueue.Dequeue)
End If

To further manage the Queue there is the Clear command that removes all of the contents of the Queue and the Count property to get the current number of items in the queue. There is also the Contains function that you can use to find out if a particular item is already in the queue. For example:

If Not FileNameQueue.Contains(NewFileName) Then
    FileNameQueue.Enqueue(NewFileName)
End If

One feature that you might find helpful at times is the ability to copy the Queue to an Array as seen here:

Dim MailList() As Net.Mail.MailMessage = MailQueue.ToArray()

That covers the basics of the .NET Generic Queue but maybe you’re wondering when you should use a queue?

I’ve found them particularly useful for situations where I had longer operations to process, such as parsing or converting a large datafile. For example, if I had files arriving in a FTP folder I would use FileSystemWatcher to detect the files as they arrived and place them in the queue. Then, at some point, such as every 30 minutes, the current items in the queue would be processed in the order they arrived and a new queue would be started.

Another example would be having a central order system prepare a queue of items for a remote fulfillment centers. The fulfillment center system would retrieve a serialized queue of orders via a web service from the order location’s web site and then work through the order objects within it.

If you have any questions about using Generic Queues or if you have suggestions or examples on how you use them, please leave a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (2 votes, average: 3 out of 5)
Loading ... Loading ...

2 Comments Add your own

  • 1. Greg  |  January 1st, 2008 at 2:52 pm

    I am writing a performance monitoring program, and I want to add the ability to keep the last few minutes of performance information. I need to add new information and drop old information. I can dequeue the oldest value and add the latest value to the top during the performance monitoring collection process. This data could then be used to generate a chart.

  • 2. hjjkhkRequired  |  September 9th, 2008 at 8:45 am

    am writing a performance monitoring program, and I want to add the ability to keep the last few minutes of performance information. I need to add new information and drop old information. I can dequeue the oldest value and add the latest value to the top during the performance monitoring collection process. This data could then be used to generate a chart.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives