Collection クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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] |
位置またはキーによって、 |
| Item[Object] |
位置またはキーによって、 |
| Item[String] |
位置またはキーによって、 |
メソッド
| 名前 | 説明 |
|---|---|
| Add(Object, String, Object, Object) |
|
| Clear() |
Visual Basic |
| Contains(String) |
Visual Basic |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetEnumerator() |
コレクションを反復処理する列挙子を返します。 |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| Remove(Int32) |
|
| Remove(String) |
|
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
明示的なインターフェイスの実装
拡張メソッド
| 名前 | 説明 |
|---|---|
| AsParallel(IEnumerable) |
クエリの並列化を有効にします。 |
| AsQueryable(IEnumerable) |
IEnumerable を IQueryableに変換します。 |
| Cast<TResult>(IEnumerable) |
IEnumerable の要素を指定した型にキャストします。 |
| OfType<TResult>(IEnumerable) |
指定した型に基づいて、IEnumerable の要素をフィルター処理します。 |