Kontakt
DSVGO
Historie | |
---|---|
01.09.2003 | FAQ/IsInCollection: Prüfung, ob ein bestimmter Key schon in einer Collection vorhanden ist |
07.06.2001 | CollectionChangeKey: Nachträgliche Änderung eines Keys hinzugefügt; FAQ begonnen |
30.04.2000 | Erste Version |
Collections werden dem VB-Einsteiger oft verschwiegen. Dabei halte ich sie für eine der nützlichsten Objekte, um mal eben Daten abzuspeichern und wiederzufinden!
Folgendes Beispiel sollte Schritt für Schritt durchgegangen und erläutert werden. Voraussetzung dafür: Ein Formular "Form1" mit einer TextBox "Text1" und CommandButton "Command1" darauf.
Collections deklarieren:
Private colStrings As Collection Private colObjekte As Collection Private colMixed As Collection
Strings in einer Collection ablegen:
Set colStrings = New Collection colStrings.Add "Bla Bla - Text 1", "s1" colStrings.Add "Bla Bla - Text 2", "s2" colStrings.Add "Bla Bla - Text 3", "s3" colStrings.Add "Bla Bla - Text 4", "s4" 'Strings abfragen: MsgBox colStrings.Count '"4" MsgBox colStrings(2) '"Bla Bla - Text 2" MsgBox colStrings("s4") '"Bla Bla - Text 4" 'Strings aus Collection entfernen: colStrings.Remove 1 colStrings.Remove "s4" MsgBox colStrings.Count '"2"
Objekte (hier Form und Controls) in Collection ablegen:
Set colObjekte = New Collection colObjekte.Add Form1, "o1" 'Formular colObjekte.Add Command1, "o2" 'CommandButton colObjekte.Add Text1, "o3" 'TextBox MsgBox colObjekte(2).Name '"Command1"
Alles mögliche in Collection ablegen:
Set colMixed = New Collection colMixed.Add "Bla bla Text 1", "m1" 'String colMixed.Add Command1, "m2" 'CommandButton colMixed.Add colObjekte, "m3" 'Collection (!) 'Elemente auslesen: MsgBox TypeName(colMixed("m3")) '"Collection" MsgBox TypeName(colMixed("m3")("o1")) '"Form1"
Anschließend sollte zumindest grob klar sein, was man mit Collections alles machen kann.
Eine komplette Collection kann mit einer einzigen Anweisung geleert werden:
Set colMixed = New Collection MsgBox "Anzahl: " & colMixed.Count '"0"
Der Key eines Collection-Elements kann normalerweise nicht verändert werden. Folgende Routine simuliert dieses, indem das entsprechende Element zum alten Key zwischengespeichert wird, das Element dann aus der Collection entfernt wird, und dann wieder mit dem neuen Key hinzugefügt wird:
Public Sub CollectionChangeKey(col As Collection, _ OldIndex As Variant, NewKey As String) Dim Value As Variant If IsObject(col(OldIndex)) Then Set Value = col(OldIndex) Else Value = col(OldIndex) End If col.Add Value, NewKey 'erst hinzufügen col.Remove OldIndex 'dann entfernen End Sub
Dann muss z.B. nur noch geschrieben werden:
CollectionChangeKey colTest, "OldKey", "NewKey"
Die folgende Funktion bestimmt mit Hilfe eines Fehlerhandlers, ob ein gegebener Key in einer Collection vorkommt. Man beachte, dass weder eine Fallunterscheidung für Objekte notwendig ist, noch eine Dummy-Variable für eine Test-Zuweisung:
Public Function IsInCollection( _ ByRef col As Collection, _ ByRef elem As String _ ) As Boolean On Error Resume Next If IsEmpty(col(elem)) Then: 'DoNothing IsInCollection = (Err.Number = 0) On Error GoTo 0 End Function
Hier ein Beispiel für die Anwendung dieser Funktion:
Sub Test() Dim c As Collection Set c = New Collection c.Add 1508, "w" 'Eine Zahl ablegen c.Add "blabla", "x" 'Einen String ablegen c.Add App, "y" 'Ein Objekt ablegen MsgBox IsInCollection(c, "w") 'True' MsgBox IsInCollection(c, "x") 'True' MsgBox IsInCollection(c, "y") 'True' MsgBox IsInCollection(c, "z") 'False' End Sub
© Jost Schwider, 30.04.2000-01.09.2003 - http://vb-tec.de/collctns.htm