Collection クラス

定義

Visual Basic の Collection は、単位として参照できる項目の順序付けされたセットです。

public ref class Collection sealed : System::Collections::IList
public ref class Collection sealed : System::Collections::IList, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public sealed class Collection : System.Collections.IList
[System.Serializable]
public sealed class Collection : System.Collections.IList, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Collection = class
    interface ICollection
    interface IEnumerable
    interface IList
type Collection = class
    interface ICollection
    interface IList
[<System.Serializable>]
type Collection = class
    interface ICollection
    interface IList
    interface ISerializable
    interface IDeserializationCallback
Public NotInheritable Class Collection
Implements IList
Public NotInheritable Class Collection
Implements IDeserializationCallback, IList, ISerializable
継承
Collection
属性
実装

次の例では、Collection オブジェクト names と、ユーザーがコレクションにオブジェクト (名前) を追加できるダイアログ ボックスを作成します。 コレクション内の名前を表示し、最後に、Collection オブジェクト自体は破棄せず、コレクションを空にします。

この動作を確認するには、プロジェクトメニューからクラスの追加コマンドを選択し、各インスタンスの名前を保持するために、nameClass (型Public instanceName) のモジュール レベルで instanceName パブリック変数を宣言します。 既定の名前は nameClass にします。 次のコードを、別のモジュールの全般セクションにコピーして貼り付けます。別のプロシージャの classNamer ステートメントを使用して起動します。 (この例は、クラスをサポートするホスト アプリケーションでのみ機能します)。

Public Class nameClass
    Public instanceName As String
End Class
Sub classNamer()
    ' Create a Visual Basic Collection object.
    Dim names As New Microsoft.VisualBasic.Collection()
    Dim key As Integer
    Dim msg As String
    Dim name As String
    Dim nameList As String = ""
    ' 1. Get names from the user to add to the collection.
    Do
        Dim inst As New nameClass()
        key += 1
        msg = "Please enter a name for this object." & vbCrLf &
              "Press Cancel to see names in collection."
        name = InputBox(msg, "Name the Collection items")
        inst.instanceName = name
        ' If user entered a name, add it to the collection.
        If inst.instanceName <> "" Then
            names.Add(inst, CStr(key))
        End If
    Loop Until name = ""
    ' 2. Create and display a list of names from the collection.
    For Each oneInst As nameClass In names
        nameList &= oneInst.instanceName & vbCrLf
    Next oneInst
    MsgBox(nameList, , "Instance Names in names Collection")
    ' 3. Remove elements from the collection without disposing of the collection.
    For count As Integer = 1 To names.Count
        names.Remove(1)
        ' Since Visual Basic collections are reindexed automatically, 
        ' remove the first member on each iteration.
    Next count
End Sub

注釈

注意

可能であれば、Visual Basic Collection の代わりにSystem.Collections.Generic名前空間またはSystem.Collections.Concurrent名前空間のジェネリック コレクションを使用してください。 詳細については、「 コレクション - C# 」または「 コレクション - Visual Basic」を参照してください。

Visual Basic Collection オブジェクトは関連する項目のグループを 1 つのオブジェクトとして参照する便利な手段を提供します。 コレクション内の項目、または要素は、コレクション内に存在するという事実によってのみ、関連付けされる必要があります。 コレクションの要素は、同じデータ型を共有する必要はありません。

次の例に示すように、コレクションは他のオブジェクトと同じ方法で作成することができます。

Dim coll As New Microsoft.VisualBasic.Collection()  

コレクションを作成した後は、次のいずれかを実行できます。

  • Addメソッドで要素を追加します。

  • Removeメソッドで要素を削除します。

  • Clearメソッドですべての要素を削除します。

  • Countプロパティでコレクションに含まれる要素の数を調べます。

  • Containsメソッドで特定の要素が存在するかどうかを確認します。

  • Item[]プロパティでコレクションから特定の要素を返します。

  • For Each...Nextステートメント コレクション全体を反復処理します。

    注意

    Visual Basic Collection オブジェクトは、Visual Basic 6.0 の Collection オブジェクトと同じ機能を持ちますが、2 つは COM 環境で相互運用できません。

    注意事項

    Visual Basic Collection の反復処理はスレッド セーフではありません。 コレクションが同期されている場合でも、他のスレッドがコレクションを変更できるため、列挙子は例外をスローします。 列挙中にスレッド セーフを保証するためには、コレクションをロックするか、他のスレッドによる変更に起因する例外をキャッチします。 プログラミング要素のロックの詳細については、SyncLock ステートメント を参照してください。

コンストラクター

Collection()

Visual Basic の新しい Collection オブジェクトを作成して返します。

プロパティ

Count

コレクション内の要素の数を取得します。

Item[Int32]

Collection オブジェクトの特定の要素を、位置またはキーに基づいて取得します。

Item[Object]

Collection オブジェクトの特定の要素を、位置またはキーに基づいて取得します。

Item[String]

Collection オブジェクトの特定の要素を、位置またはキーに基づいて取得します。

メソッド

Add(Object, String, Object, Object)

要素を Collection オブジェクトに追加します。

Clear()

Visual Basic の Collection オブジェクトの要素をすべて削除します。

Contains(String)

Visual Basic の Collection オブジェクトに特定のキーを持つ要素が含まれているかどうかを示す値を返します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

コレクションを反復処理する列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(Int32)

Collection オブジェクトから要素を削除します。

Remove(String)

Collection オブジェクトから要素を削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

Collection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。

ICollection.Count

このコレクション内の項目の数を取得します。

ICollection.IsSynchronized

Collection オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

ICollection.SyncRoot

Collection オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

IDeserializationCallback.OnDeserialization(Object)

Collection オブジェクト グラフの逆シリアル化が完了した後で実行されます。

IEnumerable.GetEnumerator()

コレクションを反復処理する列挙子を取得します。

IList.Add(Object)

項目をコレクションに追加します。

IList.Clear()

Collection オブジェクトからすべての項目を削除します。

IList.Contains(Object)

Collection オブジェクトに特定の値が格納されているかどうかを判断します。

IList.IndexOf(Object)

Collection オブジェクト内での指定した項目のインデックスを調べます。

IList.Insert(Int32, Object)

Collection オブジェクト内の指定されたインデックス位置に項目を挿入します。

IList.IsFixedSize

Collection オブジェクトが固定サイズかどうかを示す値を取得します。

IList.IsReadOnly

Collection オブジェクトであるかどうかを示す値を取得します。

IList.Item[Int32]

指定したインデックスにある要素を取得または設定します。

IList.Remove(Object)

指定したオブジェクトが Collection オブジェクト内にあるときに、最初に出現したものを削除します。

IList.RemoveAt(Int32)

指定されたインデックスの Collection オブジェクト項目を削除します。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Collection オブジェクトをシリアル化するために必要なデータを返します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象