![]() | |||||||||||||||||||||
![]() |
|
|
By Derrald Farnsworth-Livingston Introduction
A collection represents a group of items that has predefined methods and properties, we will discuss this in more detail later. We will focus on collections as they are used to hold objects.
Let's Get Started
Lets create an object to use. For instance, perhaps we want to have a candy bar. Our candy bar will have various properties associated with it, nuts, chocolate, coconut, etc. To create our candy bar object we should create a new class in our Visual Basic DLL. We will call this clsCandyBar. It will have three properties, the first will be the name of the candy bar, the next will be the main kind of candy bar (chocolate, sugar, etc.) the second being a filling (caramel, coconut, etc.)
That will be our basic definition for now. We can add more properties and methods to this class later.
Now that we have a candy bar we need a way to distribute these candy bars in order to turn a profit. There is no better way then with a vending machine. Because a vending machine holds a multitude of candy bars and different ones to boot we need a way to contain multiple objects. We can accomplish this task by using collections.
Like I said before, a collection represents a group of items that has predefined methods and properties. This includes the Count property, the Item method, and the Add method, and the Remove Method. Two values are associated with each object in a collection. The position of the object is the first value and the key is the optional second value. The key value is a string that is used to uniquely identify the object.
Overview of the properties and methods associated with a collection object:
Using Collections
A collection has a number of advantages that makes it ideal for many situations. For instance, a collection can handle any data type, the keys are user-defined strings making it easier to reference, a large amount of data can be stored and transferred using collections. However, because of their robust nature the memory requirements of collections are fairly high and may not be necessary in certain cases.
Creating a collection
Let’s say we have a vending machine and in this vending machine are various candy bars. We could create the vending machine as the collection of the candy bar object. We will go ahead and “build” the vending machine.
|
Section 2: Creating a Collection
| ||||||||||||||||||||
Counter