Programming with Objects: Class Libraries and Reusable Code
Objectives Understand and employ the Visual Basic.NET classes, forms, components, modules, structures, and namespaces Understand the notion of a class library and its usefulness in creating reusable code Convert a Windows Forms application into a class library Build a class library, and create a solution with multiple projects Incorporate a component into a class library project, and use the component to exchange parameter data with a client program
Understanding Classes, Forms, Components, Modules, Structures, and Namespaces Principle of structured programming –Large programs should be subdivided into smaller, meaningful logical functional units Class –Defines the properties, fields, methods, and events that go into an object –A reference type –Object is an instance of a class –Can inherit from another class –Is both inheritable and reusable Form –A type of class –Has a graphical component and a code component –May serve as a container for other forms –Must be instantiated as an object before it can be used –Code portion of a Windows form is loaded at the same time and on the same computer as the GUI
Component A class that conforms to a certain standard for interacting with other components May or may not contain a graphical element Can provide an easy way to communicate between projects in a solution After component class has been instantiated, its methods are available
Module A reference type, consisting of declarations and procedures only Similar to a class in terms of serving as a container for code Unlike a class in several respects –Cannot be instantiated nor inherited –Cannot be nested inside another structure Variables and procedures are shared by default Public variables and procedures –Accessible and scoped to the namespace that contains the module
Structure An expansion of the user-defined type in previous releases of Visual Basic Can contain different kinds of data items, each with its own accessibility Is a value type (each variable based on a structure contains its own data) Declared at the module or class level
Declaration of structLogonPassword
Namespace Organizes the list of names that occur in an assembly An assembly –The package that results from compilation Can also contain lower-level namespaces Reason for subdividing large project into namespaces –To avoid name collisions
Compiling a Project into a Class Library Class library –Defines a class –Great way to create reusable objects –Compiles to a DLL file (dynamic link library) DLL file –Cannot be executed directly –Must be called from inside another running program Output from the compiler is known as an assembly After you have compiled your project into a class library –Executable project (or client) that calls this library must identify both the class library’s DLL file and its namespace name
Summary Class –Template or blueprint for an object Form –Class that has both a graphical representation and a code segment Component –Class designed for interaction with other components Module –Contains code only and its data values are shared Structure –Value type that defines fields, properties, methods, and events at the module level Namespace –Defines a list of names (classes, structures, other namespaces, etc.) within an assembly Class library –Defines a reusable program Project compiled into a class library –Can receive ByVal or ByRef parameters from the calling (client) program Method of passing parameters between programs –Through component class