350 Using a Collection When you add data to a collection, you can optionally also assign a key to that data, which you can later use to search and retrieve that data again. So if you wanted to add the data Mike Ross along with the key moron, you could use the following command: HitList.Add (“Mike Ross”, “moron”) When adding a key to data in a collection, your key must meet these criteria: ✦ You must add a key at the same time you add data to a collection. After you add data to a collection, you can’t go back later and add a key to that data. ✦ Every key must be a string. ✦ Every key must be unique; no two items in a collection can share the same key. Searching and retrieving data After you store data in a collection, here are two ways to search and retrieve data from that collection: ✦ Use the index number of that data. ✦ Use the key of that data. If you don’t store a key with data originally, you can’t retrieve that data with a key. Index numbers To retrieve data based on its location, you can do something as simple as the following: Dim Junk as New Collection Junk.Add (3.1415) Junk.Add (99) Junk.Add (“Bo”) If you wanted to retrieve the name Bo from the collection, you’d have to know that Bo is stored as the third item (index number 3), so the following would store the string Bo in the Good variable: Good = Junk.Item(3) The problem with relying on index numbers alone is that as you add and delete items from a collection, the index numbers may change, as shown in Figure 3-6.
Using a Collection 351 Figure 3-6: Retrieving data by index numbers is unreliable because they can change. 1 2 3 3.1415 99 Bo 1 2 3 3.1415 99 Ollie Bird 4 Bo Originally, the “Bo” string is located at index 3. But if new items are added to the front of the collection, the “Bo” string may get placed in a different location such as at index 4. Because index numbers don’t always stay matched with each item in a collection, a better solution is to assign a unique string, or a key, to each item, as described in the following section. Keys By assigning a descriptive key to each item, you can use that key to retrieve that item no matter where it might be stored in the collection. The following code assigns the key “pi” to the first item, the key “secret agent” to the second item, and the key “my cat” to the third item: Dim MoreJunk as New Collection MoreJunk.Add (3.1415, “pi”) MoreJunk.Add (99, “secret agent”) MoreJunk.Add (“Bo”, “my cat”) To retrieve items from a collection with a key, you have to remember the key associated with each chunk of data. The following code stores the number 3.1415 into the CircleSecret variable: Book III Chapter 3 Collections and Dictionaries CircleSecret = MoreJunk.Item (“pi”) The preceding code tells the computer to find the chunk of data assigned the “pi” key and then store that data in the CircleSecret variable. The preceding code retrieves the number 3.1415 no matter where its location may be in a collection. You can always retrieve data with either its key or its location (index number).
Agreed in 2016, the motive of the General Data Protection Regulation (GDPR) is to better protect the personal data of European Union “data subjects” – EU citizens and other nationals physically present in the EU at the time data are collected. Visit: https://www.hipaajournal.com/gdpr-training/