160 likes | 256 Views
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”)
E N D
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.
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.
Array Examples • Here are a few array examples. • For more information on arrays, go to the Stephens book, pp. 620 to 623.
.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)
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!
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)
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.
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.
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.
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:
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!
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.
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)
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).
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: