|
|
Sign In Link to us |
|
Home 1. Introduction 2. Introducing VB 3. Projects 4. Containers 5. Controls 6. Code 7. Forms 8. Debugging Code 9. Error Handling 10. Objects 10.1 External objects 10.2 Hungarian Notation 10.3 Object Variables 11. User Controls 12. Data Projects 13. API 14. Distribution
|
10. Objects10.1 Referencing external objectsObject types can be added to your project from external libraries. Since types can exist within many kinds of project, object types can be added from Executable files (".exe"), Dynamical Link Libraries (".dll"), ActiveX controls (".ocx"), Object Libraries (".olb") or Type Libraries (".tlb").To include the object type in your project, you have to create a Reference to it. Once the Reference is in place, you will be able to declare instances of object variables of the same type defined within the external library.
Exercise:
Dim objMyAccessObject As Access.Application Note:
Notice that as you type the line above, Visual Basic prompts you to insert a type from a pop-up list of currently installed types.
Once you enter the dot (".") after "Access" , Visual Basic prompts you again for the types within the "Access" type library.
10.2 Object Variable Hungarian NotationAs with variables, objects can be named anything you like, as long as you don't use a reserved word. However, the Hungarian Notation model referred to earlier also extends to object names, and again the prefix is used to indicate the object type.
These are only proposed prefixes, so you don't necessarily have to stick to them. 10.3 Object VariablesWhen an external object is referenced, instances of the object can be created with Object Variables. These special variables are declared as the type specified by the original object (e.g. If you want a new instance of an ADODB object, you would declare it as type ADODB).Once the variable is declared as that type, Visual Basic knows what system resources (memory etc) to reserve for this object.
Example: Creating a new ADODB data control recordset
Dim m_objADODBRec1 As ADODB.Recordset
Private Sub Form_Load()
-- etc --
Set m_objADODBRec1 = New ADODB.Recordset
-- etc --
End Sub
To point the object variable to the object, you must use the Set keyword. The New keyword tells Visual Basic to point the object
variable "m_objADODBRec1" to a new instance of the object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||