1 / 16

Collections

Collections. Two Hierarchies. Recall that the architecture of an object-oriented program consists of two hierarchies: The Class Structure (“is a”) The Object Structure (“has a” or “has some”)

hanzila
Download Presentation

Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collections

  2. Two Hierarchies • Recall that the architecture of an object-oriented program consists of two hierarchies: • The Class Structure (“is a”) • The Object Structure (“has a” or “has some”) • One of the most important ways to implement the object structure in VB is using the built-in collection classes. • Objects of one class can contain one or more collections of objects from other classes.

  3. Arrays • Arrays are useful and familiar—they are the type of collection that has been around the longest. • Arrays have limitations: • Resizing an array requires the use of ReDim (and ReDim Preserve if there’s already data in the array that you want to save) • Inserting items into the middle of an array involves not only using ReDim Preserve, but moving all later elements back one in the array. • All items in an array must be of the same data type.

  4. Array Examples • Here are a few array examples. • For more information on arrays, go to the Stephens book, pp. 620 to 623.

  5. .NET Collection Classes • One of the nicest things that Microsoft programmers have ever done for us was to create a bunch of collection classes as part of the .NET framework. • BTW, the .NET framework is the huge collection of classes that are available to you when you write your VB programs (the same classes are also available to C# programmers)

  6. Collection Classes • Chapter 28 of the textbook provides a detailed look at the many specialized collection classes available in VB, including ArrayList, LinkedList, HashTable, and more. • Each one encapsulates a certain type of behavior that programmers used to spend hours or days coding into their programs. • I will show you the collection classes that I have found most useful, but you should be aware that these other types exist—one of them may be just what you need for your project!

  7. Non-Generic Collections • The generic collections are much easier to use, since they require all objects to be of a single type. • I prefer generic collections in my own code. • However, you will find non-generic collections used throughout the .NET framework, so you will need to know how to use them. • The most important non-generic collections you need to know about are: • The Items collection of a ListBox or ComboBox; • The Controls collection of a Form or container control (GroupBox, Panel, TabControl tab, for example)

  8. The Items Collection • ListBox and ComboBox controls have a property called “Items”. • Items is a non-generic collection; it can hold all types of objects. • Note: The meaning of “generic” here is pretty much the opposite of its meaning in English. Generic VB collections hold specific data types, while non-Generic collections hold any data type. • Technically speaking, Items holds the addresses of the objects. • So what do you see in the ListBox/ComboBox when you add a complicated object to Items? • You see the value of the object’s ToString function.

  9. Items Collection • Okay, now we know how to add objects to ListBoxes and ComboBoxes, and have them say something meaningful. • Why’d we do that? • Both ListBoxes and ComboBoxes have a SelectedItem property. • ListBoxes also have a SelectedItems collection, which is just the subset of Items that the user has selected.

  10. SelectedItem • Because you can put any type of object into a ListBox or ComboBox, we don’t necessarily know a lot about what type of object has been selected. • Look what happens when I try to write code using SelectedItem: • I can’t access SelectItem’sFirstName or GPA properties, because I’ve allowed any type of object into the ListBox, and I’m not sure that the user has picked one of type Student. • To be able to access Student properties of SelectedItem, I have to: • Make sure that SelectedItemis a Student; and • Treat (cast) it as a Student.

  11. TypeOf and DirectCast • To verify that SelectedItem is a Student (or whatever class you want to check for), use TypeOf…Is. • Once you know that it’s a student, use DirectCast to treat it as one. • Example:

  12. Too much code? • While the flexibility of being able to put all types of objects into the Items collection (or any non-generic Collection class) is appealing, the fact that you have to use TypeOf and DirectCast all the time makes writing the code more difficult and time-consuming. • In addition, since SelectedItem can be any type of object, Intellisense can’t help you out. • It would be nice if we could make collections of objects of the same type—less flexible, but a lot easier!

  13. Enter Generics • This is where the Generic collections come in. • Generic collections allow you to specify what data types are allowed into the collection. • When you create a generic collection of a specific type, you can put in: • Objects of that class; or • Objects of any class inherited from the specified class. That is, if you make an Automobile class, you can add Fords, Toyotas and Volkswagens to it (assuming those classes inherit from Automobile). • You can also create collections of an Interface type. These can hold objects of any class that implements the Interface. • You will use at least one collection of an Interface type (ThingsToDraw) in Assignment 3.

  14. Generic Collections • As I said earlier, chapter 28 describes many different types of collections. • We’ll focus on these: • Generic.List (today) • Generic.Queue (later) • Generic.Stack (later) • Generic.Dictionary (later)

  15. Generic.List • A Generic.List is basically an array on steroids (without the negative side effects). • It is strongly typed, so you generally won’t need to use TypeOf and DirectCast with items in the List. It will only allow you to add objects of the correct type. • You can add elements at the end of the list (Add), or anywhere else (Insert). • You can check to see if an item is already in the List (contains), or search for items matching certain criteria (Find). • You can easily convert the list to an array if you need to pass it as a parameter to some procedure (add “.ToArray” to the end of the List’s name).

  16. Looping Through Lists • Because the elements in a Generic.List are all of the same type, you can loop through them without checking their types. • This makes the code much simpler:

More Related