次の方法で共有


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... を使用してコレクション全体を反復処理します 。次のステートメント

    Visual Basic Collection オブジェクトの機能は Visual Basic 6.0 の Collection オブジェクトと同じですが、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 オブジェクトのシリアル化に必要なデータを返します。

拡張メソッド

名前 説明
AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

適用対象