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 개체는 관련된 항목 그룹을 단일 개체로 참조하는 편리한 방법을 제공합니다. 컬렉션의 항목 또는 요소는 컬렉션에 존재한다는 사실과만 관련이 있어야 합니다. 컬렉션의 요소는 동일한 데이터 형식을 공유할 필요가 없습니다.
다음 예제와 같이 다른 개체를 만드는 것과 동일한 방식으로 컬렉션을 만들 수 있습니다.
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을 반복하는 것은 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정하여 열거자가 예외를 throw할 수 있습니다. 열거 중 스레드 안전을 보장하려면 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch합니다. 프로그래밍 요소 잠금에 대한 자세한 내용은 SyncLock 문을 참조하세요.
생성자
| Name | Description |
|---|---|
| Collection() |
새 Visual Basic Collection 개체를 만들고 반환합니다. |
속성
| Name | Description |
|---|---|
| Count |
컬렉션의 요소 수를 가져옵니다. |
| Item[Int32] |
위치 또는 키로 개체의 |
| Item[Object] |
위치 또는 키로 개체의 |
| Item[String] |
위치 또는 키로 개체의 |
메서드
| Name | Description |
|---|---|
| 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) |
명시적 인터페이스 구현
확장명 메서드
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
쿼리의 병렬 처리를 사용하도록 설정합니다. |
| AsQueryable(IEnumerable) |
IEnumerable IQueryable변환합니다. |
| Cast<TResult>(IEnumerable) |
IEnumerable 요소를 지정된 형식으로 캐스팅합니다. |
| OfType<TResult>(IEnumerable) |
지정된 형식에 따라 IEnumerable 요소를 필터링합니다. |