View
224
Download
0
Tags:
Embed Size (px)
Citation preview
Accessing the Values of a Class• The Property Get procedure retrieves a property from a
class, making it available to the outside world.
• The Property Let procedure assigns a value from the outside world to a property of the class.
• Property Let and Property Get are the only way to assign and retrieve property values for a class.
• Otherwise, there is no other “doorway” through which they can pass.
Creating a New Object Using a Class
• Creating a class module defines a new class• Code that creates and uses objects is placed in
form and code modules, whereas a class must be placed in a class module
• Create an object of a class with the New keyword—called instantiating an object
• The New keyword creates a new instance of a class when the object is first used
• Dimensioning objects with the New keyword:– Dim Employee As New cEmployee– Private mInventory As New Cinventory
• Next, create a form that instantiates an object of the class CProduct
Using a For Each… Next LoopPrivate Sub cmdExit_Click()
‘this procedure will unload all forms that are currently loaded‘in the project
Dim OneForm As Form
For Each OneForm in FormsUnload OneForm
Next
End
This is a very useful piece of code for quitting a project
Disabling Controls using a For Each…Next Loop
Dim AnyControl As Control
For Each AnyControl in frmMain.ControlsAnyControl.Enabled = False
Next
Uses the controls collection to change a property of multiple controls
10
Example of Inheritance in VB.NET
• Examine 1st line of code for a form in the Editor (look at Visual Studio 2005 in the labs)
Public Class Form1Inherits System.Windows.Forms.Form
Inherited Class, Derived Class Subclass, Child Class
Base Class, Superclass, Parent Class
Collections
• A Collection Class holds references for a series of objects created from the same class or from different classes
• Actually a collection holds references to the objects– You reference by Index Number or a Key– Similar to list box and the associated items in the
list box
Key for Collection Objects
• Key must be a string• Can be used to reference individual objects in
the collection• Declare the Key as String at the module level
of the Class module for the object (not the collection)
• Add Property Get and Let procedures
Create a collection by writing a new class module and then declaring an object variable (to create an object).
Each object in the collection is known as an item.Each object has a position known as its index.A collection is declared with a Dim, Public, or Static
statement just like any other object variable:Dim mProducts As New Collection
VB always updates the Count property of a collection every time you add or remove an item from a collection.
Collections
The Add method adds an item to the collection.mProducts.Add Prod1
The Remove method removes an item according to its index in the collection.mProducts.Remove 2
It is best to provide a unique key to each object in a collection, rather than rely upon an index (see remove example above). It is difficult to associate an index with a particular product. Using a key value makes locating an object much easier.
Add a string in the CProduct class: Private mstrProductCode As String
Use the string to add an object to a collection or remove it from a collection
Collections