260 likes | 379 Views
Windows Software Development Lecture 7. MIS288 Instructor – Larry Langellier. Where Are We?. Last Lecture Introduction to Component-Based Programming Classes, Properties and Methods Implement polymorphism using abstract classes Tonight – Creating a Collection Hierarchy
E N D
Windows Software DevelopmentLecture 7 MIS288 Instructor – Larry Langellier
Where Are We? • Last Lecture • Introduction to Component-Based Programming • Classes, Properties and Methods • Implement polymorphism using abstract classes • Tonight – Creating a Collection Hierarchy • Create a collection using the Collection object • Create an object hierarchy • Using a Public collection • Using a Private collection • Create a collection class • Implement default properties and methods • Working with Project Groups • Enumerate a collection – For Each • Creating a Persistent Collection Hierarchy
Designing an Object Hierarchy • Model the Real World • Implement collections allowing the developer to add, change, and delete collection object references • Support For Each enumeration • Example:
Collection Object • Stores object references • Add method adds an object reference to a collection • Item method returns an object reference • Remove method removes a reference • Count method gets the number of object references
Add Method • object.Add item, key, before, after • object is a Collection object • item contains the object reference to add • key contains string key to reference the object • Optional before, and after arguments are mutually exclusive and determine numerical position relative to another item
Add Method (Example) Dim Things As New Collection ' Assume Thing represents a class Dim t as New Thing Things.Add t, "T1" Object String key
Item Method • Default method for collection • object.Item(index) • Index can be string key or numeric index. ' Use string key Dim tRef As Thing Set Thing = Things.Item("T1") Set Thing = Things("T1") ' Use numeric index Set Thing = Things.Item(1) Equivalent statements
The Three Little Pigs create collection classes • House of straw • Write code in the form module to manage the collection • The developer has direct access to the collection and is able to add invalid objects • House of sticks • Create a collection inside a “container” class with methods for adding and removing objects • Implementation is hidden from the developer • House of bricks • Create a separate class to manage the collection • Polymorphism is supported – methods of different collections share the same name
Implementing the House of Straw • Declare a collection in a form module • Form module responsible for adding and removing collection items • Class module contains properties and methods pertaining to the underlying object • Problems • Invalid objects can be added to the hierarchy • Uncontrolled access to the collection • Must write logic everywhere you need to use the collection
Implementing the House of Sticks • Improvement upon the house of straw • Collection becomes private and is hidden in the class • Author creates methods to add, remove, and locate objects in the collection • Most of the code in the form module is moved to the class module • Problems • Requires unique method names for adding to different collections • Can’t write a For Each iterator for the collection
Implementing the House of Bricks • House of bricks is the way to best model a collection • Each collection defined in its own class module referred to as a collection class • Collection is hidden in the class and accessible via the methods and properties defined in the collection class • Example • Ch10_c.vbg • cmdAddWriter_Click • Writers collection class, Writer class, and Titles/Title classes • Publisher class
Default Methods and Properties • Each class module can have one default method or property • Default method or property defined by the Procedure Attributes dialog • To select the default method or property, select the class module in Code window and then activate Procedure Attributes dialog (from Tools menu)
Default Methods and Properties Property or Method name Select Default as Procedure ID
Enabling For Each Support • Current collection is hidden in the collection class so the collection cannot be enumerated by other modules • Enabling For Each support involves exposing the Private collection’s enumerator • Collections support a tiny object called an enumerator • Remember all classes support the IUnknown interface • Procedures are called by Procedure ID rather than by name • Procedure ID for the Enumerator is -4 • Procedure name is _NewEnum
Enabling For Each Support (cont.) • Exposing the enumerator is tricky • _NewEnum procedure contains an illegal character • Tools -> Procedure Attributes • Set Procedure ID to -4 Public Function NewEnum() As Iunknown Set NewEnum = mcolEmployees.[_NewEnum] End Function Brackets allow use of illegal character
Class Builder Utility • Implemented as Visual Basic Add-In • Useful to build prototypes for object hierarchies • Use for: • Building new collections • Creating classes and their properties and methods
Just Do It! • Work on Exercise 10.3 from the textbook (pages 622-623) • Focus on steps a – e, we’ll focus on the rest later • There is an executable demo (JDI1.exe) located in the Classroom Lecture Demos – feel free to run that to get a better idea of what you’re being asked to do • We will discuss the solution after you’ve worked on problem for a while • Call me over if you have questions – don’t sit there stumped for long
Project Groups • Visual Basic can run multiple projects in-process within the same instance • Add multiple projects to a project group • File -> Add Project • Original projects remain untouched • Project groups simply contains a list of projects • Project group files have a suffix of .vbg • Setting the Startup Project • Right mouse on a project • Select the “Set as Start Up” option
Project Types • Until now, all projects created have been Standard .exe projects • Visual Basic supports other project types • ActiveX DLL servers • ActiveX EXE servers • Other project types (ActiveX controls and ActiveX documents) are the subject of later chapters • Servers (libraries) provides services to other programs • They typically do not have a visual component • Contain code components • Clients connect to servers • Examples – ADO, Microsoft Word, etc.
ActiveX Servers • DLL servers run as in-process components • DLL’s load faster than EXE components • EXE servers run as out-of-process components
Setting Project Types • Project type can be defined when project is created • Project type can be set using the Project Properties dialog • Note: some project types cannot be set using Project Properties dialog
Class Instancing • Classes support the Instancing property • Instancing determines whether other projects can create an instance of the specified class • Implicitly defines whether objects can be created using the New keyword • Private - Other projects cannot reference or create an object instance • PublicNotCreatable - Other projects can reference class but cannot create class instance • Following statement would be illegal Dim txtCurrent As New TextBox
Class Instancing (cont.) • SingleUse pertains only to ActiveX EXE servers – a new process started for each instance • MultiUse - only one instance of the server is created and shared by all processes using the server • This is suitable for most cases where objects can be externally created • GlobalSingleUse, GlobalMultiuse – similar to above, except a predefined instance is created
Just Do It! • Complete Exercise 10.3 from the textbook (pages 622-623) • Focus on steps f – j to complete the solution • There is an executable demo (JDI1.exe) located in the Classroom Lecture Demos – feel free to run that to get a better idea of what you’re being asked to do • We will discuss the solution after you’ve worked on problem for a while • Call me over if you have questions – don’t sit there stumped for long
Discussion • How would you persist a “House of Bricks” object hierarchy? • When would load records from the database? • When would you save information back to the database?
What Next? • Next Week • Read Chapters 11 and 12 • Creating an ActiveX Control • Extending ActiveX Control Features • HW #7 is due at the beginning of class • DO NOT use BindingCollections in your solution • DO NOT use the techniques covered in Section C • Create a “House of Bricks” hierarchy and ADO programming to persist to a database table