Collection Sınıf
Tanım
Visual Basic Collection
, birim olarak başvurulabilen sıralı bir öğe kümesidir.A Visual Basic Collection
is an ordered set of items that can be referred to as a unit.
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
- Devralma
-
Collection
- Öznitelikler
- Uygulamalar
Örnekler
Aşağıdaki örnek, Collection
names
bir kullanıcının koleksiyona nesneler (adlar) ekleyebileceği nesne ve iletişim kutusu oluşturur.The following example creates the Collection
object names
and a dialog box with which a user can add objects (names) to the collection. Daha sonra koleksiyonda adları görüntüler ve son olarak nesnenin kendisini elden çıkmadan koleksiyonu boşaltır Collection
.It then displays the names in the collection, and finally empties the collection without disposing of the Collection
object itself.
Bunun nasıl çalıştığını görmek için, Proje menüsünden Sınıf Ekle komutunu seçin ve instanceName
nameClass
her bir Public instanceName
Örneğin adlarını tutmak için modül düzeyinde (tür) adlı bir genel değişken bildirin.To see how this works, choose the Add Class command from the Project menu and declare a public variable called instanceName
at the module level of nameClass
(type Public instanceName
) to hold the names of each instance. Varsayılan adı olarak bırakın nameClass
.Leave the default name as nameClass
. Aşağıdaki kodu kopyalayıp başka bir modülün genel bölümüne yapıştırın ve sonra classNamer
başka bir yordamdaki ifadesiyle başlatın.Copy and paste the following code into the General section of another module, and then start it with the statement classNamer
in another procedure. (Bu örnek yalnızca sınıfları destekleyen ana bilgisayar uygulamalarıyla birlikte kullanılabilir.)(This example works only with host applications that support classes.)
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
Açıklamalar
Not
Mümkün olduğunda, ad alanındaki genel koleksiyonları System.Collections.Generic veya System.Collections.Concurrent Visual Basic yerine ad alanını kullanmanız gerekir Collection
.Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of a Visual Basic Collection
. Daha fazla bilgi için bkz. koleksiyonlar-C# veya koleksiyonlar-Visual Basic.For more information, see Collections - C# or Collections - Visual Basic.
Visual Basic Collection
nesnesi, ilgili öğe grubuna tek bir nesne olarak başvurmak için kullanışlı bir yol sağlar.The Visual Basic Collection
object provides a convenient way to refer to a related group of items as a single object. Bir koleksiyondaki öğelerin veya öğelerin yalnızca koleksiyonda bulundukları olgu ile ilişkili olması gerekir.The items, or elements, in a collection need only be related by the fact that they exist in the collection. Bir koleksiyonun öğelerinin aynı veri türünü paylaşması gerekmez.Elements of a collection do not have to share the same data type.
Aşağıdaki örnekte gösterildiği gibi, başka nesneler oluşturduğunuz gibi bir koleksiyon oluşturabilirsiniz.You can create a collection the same way you create other objects, as the following example illustrates.
Dim coll As New Microsoft.VisualBasic.Collection()
Bir koleksiyon oluşturduktan sonra aşağıdakilerden birini yapabilirsiniz:Once you have created a collection, you can do any of the following:
Yöntemiyle bir öğesi ekleyin Add .Add an element with the Add method.
Yöntemi ile bir öğesi kaldırın Remove .Remove an element with the Remove method.
Tüm öğeleri Clear yöntemiyle kaldırın.Remove all elements with the Clear method.
Özelliği ile koleksiyonun kaç öğe içerdiğini öğrenin Count .Find out how many elements the collection contains with the Count property.
Yöntemiyle belirli bir öğenin mevcut olup olmadığını denetleyin Contains .Check whether a specific element is present with the Contains method.
Özelliği ile koleksiyondan belirli bir öğe döndürün Item[] .Return a specific element from the collection with the Item[] property.
Her for each ile koleksiyonun tamamında yineleyin ... Sonraki Ifade.Iterate through the entire collection with the For Each...Next Statement.
Not
Visual Basic nesnesi,
Collection
Visual Basic 6,0 ' deki nesneyle özdeş işlevlere sahip olsa daCollection
, IKISI bir com ortamında birlikte çalışamaz.Although the Visual BasicCollection
object has functionality identical to theCollection
object in Visual Basic 6.0, the two cannot interoperate in a COM environment.Dikkat
Visual Basic arasında yineleme
Collection
, iş parçacığı güvenli bir yordam değildir.Iterating through a Visual BasicCollection
is not a thread-safe procedure. Koleksiyon eşitlense de, diğer iş parçacıkları koleksiyonu değiştirebilir ve Numaralandırıcının bir özel durum oluşturmasını sağlar.Even if the collection is synchronized, other threads can still modify the collection, causing the enumerator to throw an exception. Numaralandırma sırasında iş parçacığı güvenliğini güvence altına almak için, koleksiyonu kilitleyin ya da diğer iş parçacıkları tarafından yapılan değişikliklerden kaynaklanan özel durumları yakalayın.To guarantee thread safety during enumeration, either lock the collection or catch the exceptions resulting from changes made by other threads. Bir programlama öğesini kilitleme hakkında daha fazla bilgi için bkz. SyncLock deyimleri.For more information on locking a programming element, see SyncLock Statement.
Oluşturucular
Collection() |
Yeni bir Visual Basic nesnesi oluşturur ve döndürür Collection .Creates and returns a new Visual Basic Collection object. |
Özellikler
Count |
Bir koleksiyondaki öğe sayısını alır.Gets the number of elements in a collection. |
Item[Int32] |
Bir nesnenin belirli bir öğesini |
Item[Object] |
Bir nesnenin belirli bir öğesini |
Item[String] |
Bir nesnenin belirli bir öğesini |
Yöntemler
Add(Object, String, Object, Object) |
Nesnesine bir öğesi ekler |
Clear() |
Bir Visual Basic nesnesinin tüm öğelerini siler |
Contains(String) |
Bir Visual Basic |
Equals(Object) |
Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.Determines whether the specified object is equal to the current object. (Devralındığı yer: Object) |
GetEnumerator() |
Toplulukta yinelenen bir numaralandırıcı döndürür.Returns an enumerator that iterates through the collection. |
GetHashCode() |
Varsayılan karma işlevi olarak işlev görür.Serves as the default hash function. (Devralındığı yer: Object) |
GetType() |
TypeGeçerli örneği alır.Gets the Type of the current instance. (Devralındığı yer: Object) |
MemberwiseClone() |
Geçerli bir basit kopyasını oluşturur Object .Creates a shallow copy of the current Object. (Devralındığı yer: Object) |
Remove(Int32) |
Nesnesinden bir öğeyi kaldırır |
Remove(String) |
Nesnesinden bir öğeyi kaldırır |
ToString() |
Geçerli nesneyi temsil eden dizeyi döndürür.Returns a string that represents the current object. (Devralındığı yer: Object) |
Belirtik Arabirim Kullanımları
ICollection.CopyTo(Array, Int32) |
Öğesinin öğelerini Collection Array belirli bir dizinden başlayarak öğesine kopyalar Array .Copies the elements of the Collection to an Array, starting at a particular Array index. |
ICollection.Count |
Bu koleksiyondaki öğe sayısını alır.Gets the number of items in this collection. |
ICollection.IsSynchronized |
CollectionNesneye erişimin eşitlenip eşitlenmediğini (iş parçacığı açısından güvenli) gösteren bir değer alır.Gets a value that indicates whether access to the Collection object is synchronized (thread-safe). |
ICollection.SyncRoot |
Nesneye erişimi eşzamanlı hale getirmek için kullanılabilecek bir nesne alır Collection .Gets an object that can be used to synchronize access to the Collection object. |
IDeserializationCallback.OnDeserialization(Object) |
Tüm Collection nesne grafiğinin serisi kaldırıldıktan sonra çalışır.Runs after the entire Collection object graph has been deserialized. |
IEnumerable.GetEnumerator() |
Toplulukta tekrarlanan bir numaralayıcı alır.Gets an enumerator that iterates through the collection. |
IList.Add(Object) |
Bir öğeyi koleksiyona ekler.Adds an item to the collection. |
IList.Clear() |
Nesneden tüm öğeleri kaldırır Collection .Removes all items from the Collection object. |
IList.Contains(Object) |
CollectionNesnenin belirli bir değer içerip içermediğini belirler.Determines whether the Collection object contains a specific value. |
IList.IndexOf(Object) |
Nesnedeki belirli bir öğenin dizinini belirler Collection .Determines the index of a specific item in the Collection object. |
IList.Insert(Int32, Object) |
CollectionBelirtilen dizindeki nesneye bir öğe ekler.Inserts an item to the Collection object at the specified index. |
IList.IsFixedSize |
Nesnenin sabit boyutta olup olmadığını gösteren bir değer alır Collection .Gets a value that indicates whether the Collection object has a fixed size. |
IList.IsReadOnly |
Nesnenin olup olmadığını gösteren bir değer alır Collection .Gets a value that indicates whether the Collection object is. |
IList.Item[Int32] |
Belirtilen dizindeki öğeyi alır veya ayarlar.Gets or sets the element at the specified index. |
IList.Remove(Object) |
Nesnesinden belirli bir nesnenin ilk oluşumunu kaldırır Collection .Removes the first occurrence of a specific object from the Collection object. |
IList.RemoveAt(Int32) |
CollectionBelirtilen dizindeki nesne öğesini kaldırır.Removes the Collection object item at the specified index. |
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
Nesneyi seri hale getirmek için gereken verileri döndürür Collection .Returns the data needed to serialize the Collection object. |
Uzantı Metotları
Cast<TResult>(IEnumerable) |
Öğesinin öğelerini IEnumerable belirtilen türe yayınlar.Casts the elements of an IEnumerable to the specified type. |
OfType<TResult>(IEnumerable) |
Öğesinin öğelerini IEnumerable belirtilen bir türe göre filtreler.Filters the elements of an IEnumerable based on a specified type. |
AsParallel(IEnumerable) |
Bir sorgunun paralelleştirilmesini mümkün hale getirme.Enables parallelization of a query. |
AsQueryable(IEnumerable) |
Bir IEnumerable öğesine dönüştürür IQueryable .Converts an IEnumerable to an IQueryable. |