.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘element’

BUG: The System.Collections.Queue.Clone method loses data while cloning objects

Symptoms
The Queue class is a Microsoft .NET Framework Class Library class. It represents a first-in, first-out collection of objects. However, if you use the Queue.Clone method to clone a queue, and if the call to the Queue.Dequeue method is made before the call to Queue.Clone method is made, the data is lost.
Resolution
A bounded buffer is used to implement the Queue class. The Clone method uses the beginning of the buffer, instead of the head pointer, as the starting point. When the Dequeue method is called, the first element is removed. However, the beginning of the buffer remains the same. Therefore, the first element appears as an empty element and the last element is lost in the cloning process.

BUG: Removing Collection Elements Takes Longer Than Expected

Symptoms
In Visual Basic 6.0, removing elements from the end of a collection takeslonger than removing elements from the beginning.
Resolution
When removing an element from a collection, Visual Basic 6.0 begins at thebeginning of the collection and traverses the collection until the desiredelement is reached, then that element is removed.

BUG: “Index was out of range” error message when you access a Visual Basic .NET 2002 collection object that implements IList with -1 Base

Symptoms
When you use the IList interface with a Visual Basic .NET collection object in Visual Basic .NET (2002) before you use the Insert property, you can insert elements in the list at index -1. If you insert an element at index -1, you receive the following error message when you try to read the element by using the Item property at index -1:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Resolution
The IList index has a base of -1 instead of zero. Therefore, you can insert an element at index -1 even though MSDN documentation states that the IList index is zero-based. However, you receive an error when you try to read the value from the IList at index -1.

ACC2000: How to Create a Collection of Collections

Symptoms
A limitation of the Microsoft Visual Basic for Applications Collection object is that it cannot contain a user-defined data type. You can achieve similar functionality, however, by creating a collection that contains the elements that you would normally define in your user-defined data type, and then storing that collection in a second collection. This article shows you how to create such a nested collection.
Resolution
To create a collection within a collection, follow these steps:Create a module, and then type or paste the following procedure:

Function CollectionCollection()Dim elementDim insideCollection As New CollectionDim outsideCollection As New Collection’ Populate the first element of the collection and add’ it to the parent collection.insideCollection.Add KEY:=”fname”, Item:=”joe”insideCollection.Add KEY:=”lname”, Item:=”smith”insideCollection.Add KEY:=”age”, Item:=12outsideCollection.Add insideCollection’ Clear the collection – prevents duplication of elements.Set insideCollection = Nothing’ Populate the second element of the collection and add’ it to the parent collection.insideCollection.Add KEY:=”fname”, Item:=”fred”insideCollection.Add KEY:=”lname”, Item:=”smith”insideCollection.Add KEY:=”age”, Item:=14outsideCollection.Add insideCollection’ Print the contents of the parent collection to the Debug window.For Each element In outsideCollectionDebug.Print element(“fname”), element(“lname”), element(“age”)NextEnd Function To test the function, on the View menu, click Immediate Window, type the following command, and then press ENTER:
CollectionCollectionNote that the contents of the collection appear in the Immediatewindow.