Coleções em Visual Basic

In general terms, a collection is an object used for grouping and managing related objects. For example, every Form has a collection of controls. (You can access this collection through the form's Controls property.) This collection is an object that represents all the controls on that form. It allows you to retrieve a control in the collection by its index, and to loop through the elements of the collection using a Instrução For Each...Next (Visual Basic).

However, there are several kinds of collections, and they differ from each other in several ways.

Different Kinds of Collections

Visual Basic also provides a Collection class, with which you can define and create your own collections. Like a form's Controls collection, the Collection class also provides you with the built-in functionality to loop through elements using For Each...Next and to retrieve elements by index. For more information, see Collection.

However, the two types of collections do not interoperate with each other. For example, the following code generates a compiler error.

Dim localControls As Collection

' The following line generates a COMPILER ERROR.

localControls = Me.Controls()

As coleções são incompatíveis porque a Controls coleção é um .NET Framework coleção, enquanto a variável localControls é um Visual Basic Collection. The two kinds of collections are implemented from different classes. Their methods are similar but not identical, and their indexing schemes are different.

Zero-Based and One-Based Collections

A collection can be zero-based or one-based, depending on what its starting index is. O meio antigo que o índice do primeiro item na coleção é 0 e a último significa que é 1. Um exemplo um zero-com base em coleção é o .NET Framework Controls coleção, discutido anteriormente na página. O Visual Basic Collectiondeobjeto é uma coleçãode exemplo de-um baseado.

Um-com base em coleções podem ser mais intuitivas para Visual Basic usuários, porque o índice varia de 1 e o valor da Count propriedade, que retorna o número de itens em uma coleção. The index of a zero-based collection, by contrast, ranges from 0 through one less than the value of the Count property. This can be appropriate when the index values are offsets from a base value or correspond to members of a zero-based enumeration.

.NET Framework collections are zero-based for the purpose of standardization. O Visual Basic Collection classe é um-com a finalidade de compatibilidade com versões anteriores.

Index and Key Values

Instâncias de Visual Basic Collection classe permitem que você acesse um item usando um índice de numérico ou um String chave. Você pode adicionar itens a serem Visual Basic Collection objetos com ou sem especificar uma chave. If you add an item without a key, you must use its numeric index to access it.

By contrast, collections such as System.Collections.ArrayList allow only a numeric index. You cannot associate keys with the elements of these collections, unless you construct your own mapping based, for example, on a String array holding the keys.

Adding and Removing Items

Collections also differ in whether or not you can add items to them, and if so, how those items are added. Porque o Visual Basic Collectiondeobjeto é uma finalidade geral-ferramenta de programação, é mais flexível do que algumas outras coleções. Ele tem um Add método para colocar os itens na coleção e um Remove método para retirar-out de itens.

Certain specialized collections, on the other hand, do not allow you to add or remove elements using code. For example, the CheckedListBox.CheckedItems property returns a collection of references to items by index, but your code cannot add or remove items from the collection. Only the user can do this — by selecting or clearing the appropriate box in the user interface. Thus there is no Add or Remove method for this collection.

Consulte também

Tarefas

Como: Criar uma coleção de objetos (Visual Basic)

Como: Criar uma matriz de objetos (Visual Basic)

Como: adicionar, excluir e recuperar itens de uma coleção (Visual Basic)

Como: Definir coleções nas suas classes (Visual Basic)

Como: Fazer iterações por uma coleção no Visual Basic

Solucionando problemas de coleções (Visual Basic)

Conceitos

Visão geral sobre inicializadores de coleção (Visual Basic)

Como gerenciar grupos de objetos no Visual Basic

Coleção de Classes do Visual Basic

Como gerenciar seus objetos com coleções (Visual Basic)