.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 ‘Collection’

Cannot View Subcollections on Child Sites

Symptoms
When you view subcollections on child sites, some subcollections may be shown in the Systems Management Server (SMS) Administrator console, and others may not appear. For example, one child site may have a specific subcollection, while another child site does not. Note that the missing subcollections are the subcollections that are created from a parent site, not those that are created locally to the child site.
Resolution
The SMS Administrator console only presents collections that are defined both in the Collections and Collection_SubCollections SQL tables. Because of timing issues, SMS Collection Evaluator and SQL Monitor may not correctly update both tables.

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: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: “System.InvalidCastException”

Symptoms
When you try to pass a Collection object fromMicrosoft Visual Basic 6.0 components to Microsoft Visual Basic 2005 or to Microsoft Visual Basic .NET, you may receive an error message. In Microsoft Visual Studio 2005, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Unable to cast object of type ‘Microsoft.VisualBasic.Collection’ to type ‘VBA.Collection’.In Microsoft Visual Studio .NET, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Specified cast is not valid.If you examine the type of the collection object that Visual Basic 2005 or Visual Basic .NET expects, you find that Visual Basic 2005 or Visual Basic .NET expects the VBA.Collection type instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the VBA.Collection type, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.This problem also occurs in other Microsoft .NET Framework-supported languages such as Microsoft Visual C# 2005 and earlier versions of .NET Framework-supported Microsoft Visual C#.
Resolution
The InvalidCastException error occurs because the Microsoft.VisualBasic.Collection type is incompatible with the VBA.Collection type. The COMException error occurs because only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of the VBA.Collection class outside a Visual Basic 6.0 application.

ACC2000: Properties Collection Returns Error for CurrentProject and CurrentData Objects

Symptoms
All object collections under the CurrentProject and CurrentData objects contain a Properties collection. However, when you try to refer to the Properties collection of any object in a collection under the CurrentProject or CurrentData object, you may receive the following error message:

Run-time error ‘2467′:
The expression you entered refers to an object that is closed or doesn’t exist.
Resolution
Although you cannot use the Properties collection to refer to properties of these objects, you can refer to the properties directly. For example, use:

Debug.Print CurrentData.AllTables(1).Name instead of:

Debug.Print CurrentData.AllTables(1).Properties(“Name”)

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.