Visual Basic An Object Oriented Approach 8 Objects
Visual Basic: An Object Oriented Approach 8 – Objects as building blocks
Structures in programs n Ways of grouping objects using available mechanisms n n Lists n n Simple lists of objects Stacks (Last-in, First out) Queues (Last-in, Last-out) Trees n n Arrays, Collections Hierarchical structures of objects Graphs n Arbitrarily interconnected objects
Arrays Private Accounts (1. . 100) As Account. Obj For I = 1 To 100 Set Accounts(I) = Account. Obj Accounts(I). Initialise Next
Logical and Physical Structure n n Interconnect objects using references Forms of interconnection involve n Logical structure n n The model of interconnections we wish to create Physical structure n The actual mechanisms we use to create them
Collection Classes n n Specialised forms of collection Based on standard collection class n n n Add Remove Item( ) Count Support for For Each … syntax n Allows natural form of iteration over a collection
Queues and Stacks n Collections with limitations on insertions/retrievals
Alternatives to Collections n Tree Structures n n n Each item can contain references to several other items Efficient for storage and retrieval Recursive
Objects and Links n Object called Data. Node. Obj Private Link As Data. Node. Obj Private Data As … Public sub Add. Data(…) … Public sub Initialise Set Link = Data. Node. Obj If. . . Link. Initialise Link. Add. Data
Graphs n Arbitrary interconnections n n Complex, and can be inefficient to code Require use of specialist algorithms to search and traverse
Visual Basic Structures End
OOP Structural Forms n In general, object orientation supports n n n Inheritance (interface only in VB) Composition (whole-part structures) Aggregation (objects with collections) Messages (interacting objects) Building an efficient, well-organised object oriented program should involve all of these to some degree
Structures in the real world n All around us, things are organised into structures n n n Buildings Traffic and communications networks Living organisms Complex systems built up of repeated of simple building blocks Should try to mimic this in programs
Modelling in VB n Tendency to favour Collections in VB code n n n Simple Efficient Direct Can be used to create a variety of logical structures The ‘Microsoft way’ Not always the best mechanism n Can build custom forms of structures using references
Building Collection Classes n Awkward to hand-code in VB n n Need to set properties for specialist methods of collection and collected classes Special members n n n Item property – Default property Iterator (For…Each enumerator) VB’s class builder n n n Generates whole collection class automatically Links collection class to collected class None of the subtle syntax errors possible in hand coding
Using a custom collection class n n n Simplifies coding for client applications Add items using a simple call Iterate over collection easily Set T = New col. Transactions T. Add “ 10/10/2000”, 150. 00, “Deposit” T. Add …. . ‘ ‘ ‘ For Each Tr In T T. Print. Statement. Line Next
Alternative Collections n Ordered Collection n Items added in a specific order Can use binary search to insert and retrieve items Efficiency in searching (and therefore retrieval
- Slides: 16