150 CHAPTER 5 STORING AND RETRIEVING APPLICATION (Web page design)
150 CHAPTER 5 STORING AND RETRIEVING APPLICATION DATA addition to retrieving items using an index position number, you can also retrieve an item using its associated key. The best way to learn about the dictionary object is to see one in action. The following statements define a dictionary object named strNamesDictionary, and then populate it with data. Dim strNamesDictionary As New Dictionary strNamesDictionary.Value(”Molly”) = “Mollisa Lee Ford” strNamesDictionary.Value(”William”) = “William Lee Ford” strNamesDictionary.Value(”Alexander”) = “Alexander Lee Ford” Look at the first statement in the previous list. It uses the Dim keyword to declare the new dictionary object. Notice it also uses the New keyword. The New keyword is required whenever you are creating a new instance of an object. More information about working with objects is available in Chapter 8. The last three statements populate the dictionary object using its Value method. The first argument passed to the Value method is a key, which is followed by the equals sign and the data to be associated with that key. Note, unlike arrays, you needn t worry about defining the size of your dictionary objects in advance. Once loaded with data, you can use any of the dictionary object s properties and methods to access and modify its contents. For example, the following statement uses the dictionary object s Count property to display the number of items currently stored in the strNamesDictionary dictionary, as Figure 5-3 shows. MsgBox “There are ” + Str(strNamesDictionary.Count) + ” key-value pairs in the dictionary” Figure 5-3. Counting the number of items stored in a dictionary, as shown on Windows If you want, you can retrieve the value of any element in the dictionary by specifying its index location, as the following shows. MsgBoxstrNamesDictionary.Value(strNamesDictionary.Key(0)) In this example, the first element stored in the dictionary object is displayed. The real power and convenience of working with a dictionary object is you can work with its data without having to specify index numbers. Instead, you can, for example, retrieve a value from the dictionary by specifying its key, as the following shows. MsgBox strNamesDictionary.Value(”Alexander”) The dictionary object provides a number other helpful methods that you can use to manage its contents. For example, you can use its Remove method to delete a specified element from the dictionary, as the following shows. strNamesDictionary.Remove(”Molly”)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.