Solucionando problemas de coleções (Visual Basic)
This page lists some common problems that can occur when working with collections.
Using the Wrong Type of Collection
Há vários tipos de coleções disponíveis para um Visual Basic developer: o Visual Basic Collection classe e as classes de coleção fornecidas pela .NET Framework. These classes are not compatible with each other. This means that if you declare a variable to be of one type of collection, you cannot assign an object of another type to that variable. Also, you can access only the methods and properties of the collection type you have declared.
The main differences between the Visual Basic and .NET Framework collection classes include the following:
Index Base. .NET Framework collections are zero-based, while the Visual Basic collection is one-based. Isso significa que os elementos de um Visual Basic coleção têm valores de índice de 1 até o valor da Count propriedade, enquanto os elementos de um .NET Framework coleção têm valores de índice de 0 a menos que o valor da coleção Count propriedade.
Tipode elemento. O Visual Basiccoleção oferece suporte a elementos do tipo Object, que não é fortemente tipado porque você pode adicionar um elemento de qualquer tipo de dados. This usually results in degraded performance because the compiler must box and unbox the elements to convert them to and from the Tipo de dados Object. Some of the .NET Framework collections also have elements of type Object, but many others are strongly typed, meaning they support elements of a specific type, which makes them type-safe and usually results in optimal performance.
Elementos com chave. O Visual Basic coleção permite que você especifique um chave quando você adiciona um elemento para ele. The key is a unique String value which you can use later to access that particular element. The .NET Framework collections vary in regard to keys. Some support keys and some do not.
The namespaces that contain the various collection class definitions are the following:
Microsoft.VisualBasic— the Visual Basic Collection classe
System.Collections — specific collection classes such as lists, queues, bit arrays, hash tables, and dictionaries
System.Collections.Generic — generic collection classes, which allow you to create strongly typed collections and specify the element data type when you create them
System.Collections.Specialized — specialized and strongly typed collection classes, such as linked-list and hybrid dictionaries, bit-vector and name-object collections, and string-only collections
Correct Approach
Determine which type of collection is most appropriate for your needs. Declare your collection variable to be of that type, and be sure to create an object of that same type. Use full qualification to ensure that you are specifying the collection type you intend to. The following example shows two declarations with full qualification.
Dim customers As New Microsoft.VisualBasic.Collection()
Dim stringQueue As New System.Collections.Generic.Queue(Of String)
Once you have created a collection of a specific type, be sure you use only the methods and properties defined on that type. Set Option Strict On to catch any incorrect object assignments or member accesses at compile time.