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

Other Resources


Affiliated Links:
www.freefunfings.com


10. Objects

10.1 Referencing external objects

Object 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:
  • Create a "Standard EXE" project within Visual Basic.
  • Select the "References." option from the "Project" menu. Scroll down the list in the dialogue box until you see "Microsoft Access 8.0 Object Library" and tick the box beside it. Click the "OK" button.
  • Double-click the default Form to open the Code View window. At the top, add declaration for a new object variable:
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 Notation

As 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.

Object TypePrefixExample
3D PanelpnlpnlGroup
ADODataadoadoBiblio
AnimatedButtonanianiMailBox
CheckBoxchkchkReadOnly
ComboBox or drop-down ListBoxcbocboEnglish
CommandButtoncmdcmdExit
CommonDialogdlgdlgFileOpen
CommunicationscomcomFax
Control (unknown type)ctrctrCurrent
DatadatdatBiblio
Data-bound ComboBoxdbcbodbcboLanguage
Data-bound GriddbgrddbgrdQueryResult
Data-bound ListBoxdblstdblstJobType
Data CombodbcdbcAuthor
Data GriddgddgdTitles
Data ListdbldblPublisher
Data RepeaterdrpdrpLocation
Date PickerdtpdtpPublished
DirListBoxdirdirSource
DriveListBoxdrvdrvTarget
FileListBoxfilfilSource
Flat ScrollBarfsbfsbMove
FormfrmfrmEntry
FramefrafraLanguage
GaugegaugauStatus
GraphgragraRevenue
GridgrdgrdPrices
Hierarchical FlexGridflexflexOrders
Horizontal ScrollBarhsbhsbVolume
ImageimgimgIcon
ImageComboimgcboimgcboProduct
ImageListilsilsAllIcons
LabellbllblHelpMessage
LinelinlinVertical
ListBoxlstlstPolicyCodes
ListViewlvwlvwHeadings
MAPI messagempmmpmSentMessage
MAPI sessionmpsmpsSession
MCImcimciVideo
MenumnumnuFileOpen
OLE containeroleoleWorksheet
OptionButtonoptoptGender
PictureBoxpicpicVGA
PictureClipclpclpToolbar
ProgressBarprgprgLoadFile
Remote DatardrdTitles
RichTextBoxrtfrtfReport
ShapeshpshpCircle
SlidersldsldScale
SpinspnspnPages
StatusBarstastaDateTime
SysInfosyssysMonitor
TabStriptabtabOptions
TextBoxtxttxtLastName
TimertmrtmrAlarm
ToolbartlbtlbActions
TreeViewtretreOrganization
UpDownupdupdDirection
Vertical scroll barvsbvsbRate

These are only proposed prefixes, so you don't necessarily have to stick to them.

10.3 Object Variables

When 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.

<<Page 9  :   Top  :   Page 11>>

©vbtraining.co.uk