Understanding Objects and Collections in Visual Basic.NET
So far, you've gotten an introduction to programming in Visual Basic by building a Picture Viewer project. You spent the previous hour digging into the IDE and learning skills critical to your success with Visual Basic. In this hour, you're going to start learning about some important programming concepts, namely Objects.
The term Object, as it relates to programming, might have been new to you prior to this book. The more you work with Visual Basic, the more you'll hear about objects. Visual Basic .NET, unlike its predecessors, is a true object-oriented language. This hour isn't going to discuss object-oriented programming in any detail, because object-oriented programming is a very complex subject and is well beyond the scope of this book. Instead, you'll learn about objects in a more general sense. Everything you use in Visual Basic is an object, so understanding this material is critical to your success with Visual Basic. Forms are objects, for example, as are the controls you place on a form. Pretty much every element of a Visual Basic project is an object and belongs to a collection of objects. All objects have attributes (called properties), most have methods, and many have events. Whether creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works. In this hour, you'll learn what makes an object an object, and you'll learn about collections.
The highlights of this hour include the following:
- Understanding objects
- Getting and setting properties
- Triggering methods
- Understanding method dynamism
- Writing object-based code
- Understanding collections
- Using the Object Browser
NOTE
If you've listened to the programming press at all, you've probably heard the term object oriented, and perhaps words such as polymorphism, encapsulation, and inheritance. In truth, these new object-oriented features of Visual Basic are very exciting, but they're far beyond Hour 2. You'll learn a little about object-oriented programming in this book, but if you're really interested in taking your programming skills to the next level, you should buy a book dedicated to the subject after you've completed this one.
Understanding Objects
Object-oriented programming has been a technical buzzword for quite some time, but as far as Visual Basic programmers are concerned, it's only now a reality with Visual Basic .NET (no previous version of Visual Basic was a true OO language). Almost everywhere you lookthe Web, publications, booksyou read about objects. What exactly is an object? Strictly speaking, it is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure's interfaces (properties, methods, and events). In reality, the answer to this question can be somewhat ambiguous because there are so many types of objectsand the number grows almost daily. However, all objects share specific characteristics, such as properties and methods.
The most commonly used objects in Visual Basic are the form object and the control object. Earlier hours introduced you to working with forms and controls and even showed you how to set form and control properties. In your Picture Viewer project from Hour 1, for instance, you added a picture box and two buttons to a form. Both the PictureBox and the Button control are control objects, but each is a specific type of control object. Another, less-technical example uses pets. Dogs and cats are definitely different entities (objects), but they both fit into the category of Pet objects. Similarly, text boxes and buttons are each a unique type of object, but they're both considered a control object. This small distinction is important.