06/03/14Application Development with MapObjects and Visual Basic Kristi Lombard Application Development with MapObjects and Visual Basic MapObjects will likely be the programming environment of choice for programmers wishing to build mapping capabilities into an application developed within a Windows based programming environment. While many GIS application developers may be familiar with ArcInfo and ArcView as an application development environment, they may be less familiar with MapObjects and the programming environments that MapObjects is designed to work within. The purpose of this paper is to provide an overview of Object Oriented Programming and to provide techniques for taking advantage of the constructs that such an environment provides. This paper then discusses specific techniques for building an application with MapObjects within a Visual Basic environment. Introduction The last several years have witnessed a significant shift toward personal computers with a Windows based environment. Complementing this shift is the advent of numerous programming environments that are designed to leverage the advantages associated with Windows. Visual Basic, Visual C++ and Delphi are examples of programming environments that enable users to quickly and efficiently create programs that are robust and easy to use. GIS programmers working in a Windows environment have several choices of programming environments. Using the NT operating system, choices include ArcInfo, ArcView or MapObjects. The Windows 95 operating system offers GIS programmers ArcView and MapObjects. Most GIS developers are familiar with ArcInfo and ArcView, but perhaps less familiar with MapObjects. MapObjects is a set of mapping and GIS components for application developers. MapObjects consists of an OLE Control and a collection of programmable OLE Automation objects. MapObjects can operate within any programming environment that supports OLE controls. MapObjects is an ideal for those wishing to work within a "visual" programming environment such as Visual C++ or Visual Basic. There are several reasons a programmer may choose to develop an application with MapObjects rather than ArcView. In some situations, MapObjects offers many benefits to developers. It provides great flexibility that allows developers to create customized applications. The smaller memory footprint, or RAM requirements, allows for streamlined applications that are quick and efficient. And, the "visual" nature enables developers to implement a RAD (Rapid Application Development) paradigm. This paper focuses on application development using MapObjects in a Visual Basic environment. First, an overview of Object Oriented Programming is provided and relevant key terms and concepts of are defined. The latter section addresses coding standards and code optimization methods for building an application with MapObjects with Visual Basic. Overview of Object Oriented Programming The theory of Object Oriented Programming (OOP) was originally formulated in the process of creating languages intended to simulate real life. The first OOP language was Simula 67, developed in the sixties to proceedings.esri.com/library/userconf/proc97/proc97/to350/pap325/p325.htm 1/9 Some languages are true OOP. Most languages are not true OOP but hybrid OOP languages. 3. The object is designed so that it is flexible to solve many problems of the same type. which involves sets of tellers. in the C programming language. such as queuing theory and discrete event simulation. such as Visual Basic.g.g. With OOP. Extensible. Robust. Often the two concepts are (incorrectly) used interchangeably. There are many benefits of using an OOP approach. yet they also allow procedural construction in programs.. reusable and persistent as the everyday object that is being modeled. The object is free of errors and safe from errors in other objects.06/03/14 Application Development with MapObjects and Visual Basic solve simulation problems. a "telephone object" in the computer should look and act like a real-world telephone. 6. 7. That is. using as few resources as possible. Simulation modeling. while units of data are called structures. the tools available to the programmer depend on the programming environment. A classic example is the bank teller problem. Code reuse encourages efficiency. however they are fundamentally different. The user of an object should never need to peek in side the "black box." Ideally in creating an object. is too complex to model in a procedural way. Hybrid languages vary in the degree to which they can be considered OOP. The object is intuitive because it is based on real world examples. The object oriented model is a set of design principles that can be implemented in any programming language. customers. The object contains code (sequences of computer instructions) and data (information that the instructions operate on). Since the object closely represents the real-world phone. The object is independent yet can interact with other objects when required. Traditionally code and data have been kept apart. For example. The object performs its task quickly. High integrity. The more powerful OOP programming languages have more built in constructs. and various events and time distributions. containing just the necessary information to relate and interact with other objects. and it should be as intuitive and easy to use.com/library/userconf/proc97/proc97/to350/pap325/p325. contain fewer OOP constructs). such as Ada and SmallTalk.esri. 4. 2. they implement some OOP concepts. the code and data are merged into an object. contain most constructs that enable OOP). There is no standard definition of OOP. The object can evolve with new requirements. Object An object is a "black box" that receives and sends messages. They range from powerful languages (e. rather than just the specific problem at hand. The object does not contradict itself nor the environment by identifying external object interactions The Basic Building Blocks of OOP Objects and classes are the basic building blocks of OOP. transactions. Efficient. Reusable and maintainable. The basic principles of the OOP paradigm are listed below: 1. Compatible. 5. units of code are called functions. Ease of use. For example. This is helpful in simulating change and growth. The object would have to change only when the realworld phone changes. it should be as functional. Each object makes as few assumptions about other objects as possible. OOP was designed to capture the event driven nature of simulation with the ability to capture all potential interactions in a short period of time. proceedings. it is easy to determine what changes should be made and where these changes should be made. such as C++ and Delphi. to less powerful (e.htm 2/9 . Behaviors are the features of objects that enable them to respond to stimuli. because each phone could have a different color. This coupling between objects leads to hiding implementation details. For example. Relationships are the features of objects that indicate their association with other objects.com/library/userconf/proc97/proc97/to350/pap325/p325. setting each other's properties and sending each other messages. reusable and maintainable code. so that objects do are not dependent on other proceedings. To reduce redundancy. The class definition. a person's hand is part of that person. As the popular adage states. that is. "Software is not written. The key to encapsulation is defining the interface that forms the boundary between the object and the environment. For example. It is important to remember that the class is only a concept and only objects can have values associated with properties. Properties are the features of objects that indicate the current state of being. Information hiding. a user is related to their telephone by ownership. especially those in the same class. there are two distinct telephone objects belonging to the same class telephone. Keep information contained within the object (private) unless it is required by another object." It is also a well-documented fact that 70% of the cost of a software system is maintenance. then. The goal is to build encapsulated objects with strong internal integrity and direct flexible access between objects (loose coupling). For example. 2.esri. objects are encapsulated at the class level. polymorphism and persistence. Objects interact with each other by calling each other's methods. which determines everything about an object. The two telephone objects also have the behavior of ringing upon an incoming call. the Color property of the telephone object is green. But the value of the color property can be assigned to an instance of the class telephone. two coworkers may each have a telephone. For example. will contain most of the information about an object. behaviors and relationships. and a large part of maintenance is making sure that the software system is in line with changes occurring in the real world. Identity. objects are easily identified.htm 3/9 . Encapsulation Encapsulation is the bundling of details of objects so that they are self contained and identifiable. behaviors and relationships. The two telephone objects thus have properties from being an instance of the class telephone. Modularity. inheritance.06/03/14 Application Development with MapObjects and Visual Basic The ability to modify software is very important. For example. Keep objects as loosely coupled as possible. For example. but a glove is not. 3. when a phone call comes in. which leads to modular. and a keypad for dialing. The object should be uniquely identified from other objects. Coupling. In the real world. the value of the Color property cannot be assigned to the class telephone. Objects have properties. handle the object in only one place and in only one way. the phone will ring. An object is an individual instance of a class. 4. Classes enable objects to be grouped based on common properties. As mentioned above. they both have a handset for the user to speak into. For example. More OOP concepts Following is a partial list of concepts that are important to object oriented programming. it is rewritten. encapsulation. Class Objects are defined by its class. There are four related concepts: 1. For example. specific types of shapes. the telephone class has the property of having a handset. whereas the circle shape may have a diameter property.06/03/14 Application Development with MapObjects and Visual Basic objects in order to function. even when the underlying implementation is different. In this manner. the programmer may often want to treat an object not as the specific type that it is but as a member of its base type. Returning to the telephone example. though the method for calculating the area will be different for each.esri. Creating persistent objects would decrease the efficiency and thus performance. When dealing with type hierarchies. A classic example is the shape class where the base type is "shape. In the shape example. Each derived shape will have the size. are more efficient with non persistent objects. objects are not generally persistent. Inheritance avoids redundancy in definition. organize code in a logical manner proceedings. The new class' events. The telephone class can have subclasses for phones of type cordless and mobile. properties and methods are derived from the original class. All shapes can be drawn and erased. color and position properties. Building an Application with Visual Basic and MapObjects There are several coding standards that can be used to increase efficiency. Polymorphism Polymorphism literally means "Many Shapes. classes can be constructed hierarchically. For example. Object should save portions of itself without explicitly being asked to do so. color. The cordless and mobile phone subclasses inherit the properties of having a handset. Persistence Persistence is the tendency for real world objects to remain in their place unless they are moved." Polymorphism is related to inheritance and allows objects to maintain the same interface and behavior across multiple classes. however. the area can be calculated for the rectangle and the circle object. Inheritance Inheritance is the ability to create a new class based on an existing class. the object will take the appropriate action.htm 4/9 . Computers. which can lead to errors. these functions simply send a message to a shape object. functions manipulate the generic shape without reference to whether it is a circle or rectangle. and position property. Sometimes different objects might take different actions given the same message. the rectangle shape can have a height and width property. From the base shape class. so there is a trade-off to consider. such as circle and rectangle. if a computer is turned off before a document is saved. In the computer model object representation. Ideally objects should be persistent so they model the real world. In addition the specific shapes may have additional characteristics. For example." Each shape has a size. Given a valid message. can be derived (inherited). Word processing documents are not persistent by nature. This allows the programmer to write code that does not depend on the specific types.com/library/userconf/proc97/proc97/to350/pap325/p325. The type hierarchy embodies both the similarities and differences between shapes. then part or all of the work will be lost. such as naming conventions. Following is a list of the scope prefix designations. be it an OOP or standard procedural application. A variable declared in a function or subroutine is only accessible from that function or subroutine. although they are not hard and fast standards. Naming standards also provide code that is easier to read and understand. Prefix Description proceedings. Part Description scope Describes the scope of the variable Example m_intSelectedFeatures type Describes the data type of the variable m_intSelectedFeatures body Describes the variable m_intSelectedFeatures The scope of a variable is the level at which the variable is accessible. user interface issues. and optimization techniques will greatly enhance the quality of the resulting code. thus reducing the number of potential bugs. If the body is made up of more than one concatenated word. Variables can also be local to the module or form. Readability is enhanced by organizing the naming convention so that the three portions of the variable name (scope. one can easily ascertain the scope. data type and intended purpose) are easily distinguished at first glance: A lower case prefix value at the beginning followed by an underscore denotes the scope. such as modularization and the use of classes are easier to implement in an OOP environment. By using Hungarian notation for variables. A local variable of this sort does not require a scope prefix. Other standards. In addition. global to the project. Hungarian notation is a widely used and easily understood naming convention. mixed casing should be used so that each word begins with a capital letter. especially when more than one developer is involved.esri.06/03/14 Application Development with MapObjects and Visual Basic and increase readability. and increasing the code reusability.htm 5/9 . or passed by reference or by value. data type and intended purpose of the variable. and the body of the variable begins with an uppercase letter. Standard naming conventions Using standard naming conventions is very important. Some standards described below.com/library/userconf/proc97/proc97/to350/pap325/p325. the use of comments. The following example illustrates this concept with the variable namem_intSelectedFeatures. thereby reducing the maintenance cost and time. can be applied to any coding endeavor. Naming standards reduces the ambiguity of source code. Prefix Description g_ Global m_ Local to the module or form {none} Local to the procedure r_ Variable passed by reference v_ Variable passed by value Following is a partial listing of data types. additional lower case values denotes the variable type. esri. Comments are especially useful during development when more than one developer may be working with a particular module or procedure. The code is easier to understand. it is best to avoid public variables. Global variables also make the code less modular and more difficult to understand. When public variables are used. the variable is defined local to the module in the General Declarations section.htm 6/9 . proceedings. It is best realized when information is kept "private" to the module. This is especially important when using more complex constructs. such as arrays and collections. or the reader may have to refer to the definition to understand the variable. Use of Comments The importance of adding descriptive comments cannot be overstated.com/library/userconf/proc97/proc97/to350/pap325/p325. creating erroneous results when another subroutine uses the variable. and map displays can be prefixed with appropriate type prefixes. Thus. This produces code that is more modular and reusable. SelectedFeatures). Objects such as command buttons.e. Module names can be prefixed as appropriate. Usually associated descriptive comments would be placed with the variable definition. objects and menu items. It may not even be the case when returning to the code several weeks later. If a called subroutine requires the variable. Comments should also be written for procedures. Even a few well-placed comments will quickly increase the readability of code. with 'cls' or 'bas'. textboxes. If there is a variable that is local to a module and it is used in several subroutines. the body of the variable name should be mixed case for readability and provide a description of the variable (i.06/03/14 Application Development with MapObjects and Visual Basic dbl Double int Integer obj Object str String var Variant lng Long bln Boolean The same convention of Hungarian notation can also be applied to the naming of forms. It is recommended that each module have a comment header that describes the intended purpose of the module. the associated comments can either be rewritten. 'txt' or 'map'. This may not always be the case when code is being shared. descriptions of any variables local to the module and references to any global variables that are used. such as 'cmd'. When the variable is used in a subroutine. Again. when possible. An alternate approach would be to define the variable the first time it is used. combined with using Hungarian notation provides code that is fairly easy to understand. the value may be modified during a particular subroutine. especially those that have variables passed in or values that it returns. and the relationship between modules is expressed more clearly. For example. It is tempting to assume that the code is self explanatory. Modularity Modularity is a key concept in OOP. For a complete listing refer to the Visual Basic Programming Reference. it can be passed to the called subroutine. form names can be prefixed with 'frm'. The Visual Basic and MapObjects syntax. modules. Include help files. try to use one control for multiple purposes. For example. This is easily incorporated with the Visual Basic environment and will create a professional looking application. The MapObjects control can appear slow when displaying a large dataset. and an OK button. it will require modification in only one place. 3. This reduces the time required to load the form and its' related module. Check for validity when text is input. enabled only when the textboxes have been properly filled in. Though they may seem small and insignificant. Robust error checking routines will create a more "user-friendly" environment for the user. User-interpreted speed can be affected by changing the programs order of events or the methods of execution. As a Windows based application. Enable controls only when using the control would be an appropriate action by the user. 1. so that one subroutine can solve various problems of the same domain. The point or polygon shape is simply passed to the subroutine where it is added to the specified shapefile. User Interface Issues There are several small yet very important details that should be tended to in creating an application.06/03/14 Application Development with MapObjects and Visual Basic Passing variables also aids in designing subroutines that are general. Check spelling on command buttons. unload. paint and repaint windows on a regular basis. a module may have one subroutine that adds a point to a shapefile and another subroutine that adds a polygon to a shapefile. Especially for more complex controls. such as comboboxes and database controls. These techniques may not actually increase performance. The controls on your form can affect the speed of these operations.com/library/userconf/proc97/proc97/to350/pap325/p325. First there is no duplication of code.htm 7/9 . Visual Basic has to load. For example. if a form is designed to capture text input from the user to capture a latitude and longitude coordinates and display the point on the map. Actual display speed is affected by the speed in which windows are loaded and painted on the screen. place one control on the form and use a control array to create the remaining controls at run time. Optimization methods Optimization techniques can be applied to both the front-end (GUI) and the back-end (code). the form should have a cancel button. 4. One technique is to load the most often used forms and keep them invisible until they are needed. This approach has several benefits. if the code needs to be modified. and return a message to the user if the text is incorrect. menu items. enabled at all times. but they give the illusion of faster performance. These two modules may be replaced by one subroutine that adds a feature to a shapefile. 2. message boxes and help files. On a form with more than one control. Second. Front End (GUI) Front-end optimization can be broken down into two categories: actual display speed and user-interpreted speed. Thus it is best to not to use too many controls on a form. This is especially important if the user has numerous textboxes to fill in. paying attention to these details will create an application that conforms to standards of Windows based applications that is more intuitive and easy to use. If more than one control of the same type will need to be loaded simultaneously. Increasing the map control's RefreshCount property will give the proceedings. The following guidelines are just a few examples of techniques to add polish to an application.esri. specify the TabIndex property so that the controls are activated in a logical order from the top to bottom and left to right. Top = 1200 End With proceedings.esri. Use the With statement to set multiple properties. Avoid the use of variants.BackColor = Blue .Caption = "Label Caption" . Variants require 16 bytes of memory.Count For I = 0 to intListCount intNum = ListBox. This enables you to reference the same control in a more efficient manner. consider the following four lines of code: x = GetValue("Toolbar") If x = TRUE Then ToolBar. which requires 8 bytes and an integer. There are several structural techniques that can be implemented to increase processing speed. set string variables to zero length (i. the following code accesses the Label control three times in order to change its properties: Label1. as opposed to a double.Count value is only evaluated once intListCount = ListBox. In addition leaving pointers that do not point to anything can cause memory leaks.Top = 1200 A With statement increases the efficiency by accessing the control once With Label1 . 1. Try to combine several statements in the same line. Following is a list of several simple guidelines for the more common structures.BackColor = Blue Label1. which only requires 2 bytes.06/03/14 Application Development with MapObjects and Visual Basic impression that the features are being drawn faster.ListItem. set strMyVar = "") to save memory. Avoid using a loop that must be evaluated each time through the loop.e.Visible = TRUE 3.Caption = "Label Caption" Label1.Count intNum = ListBox. Back End (Code Optimization) Appropriate use of variables will increase the speed and efficiency of the program. For example. For a more exhaustive list of guidelines for optimizing code. When not in use.htm 8/9 .Visible = TRUE End If This one line of code performs the same action: If GetValue("ToolBar") Then ToolBar.ListItem.com/library/userconf/proc97/proc97/to350/pap325/p325.Index Next I The above loop can be changed so that the ListBox. This also will free memory back to the system. such as the following: For I = 0 to ListBox.Index Next I 2. For example. Similarly set all other object pointers to Nothing at the end of the procedure. refer to any book on intermediate or advanced Visual Basic programming. Next loop.Name Next Conclusion MapObjects is a powerful programming tool that can enable developers to incorporate mapping capabilities in their application. Use the For Each. "What is Object-Oriented Software?". VA. 1996. Dim objCtrl as Control dim strCtrlName as String For Each objCtrl in Form1. References Bettone. gifford. and usually faster. As the name implies. Kristi Lombard GIS Applications Programmer TASC 12100 Sunset Hills Drive Reston.. D.htm 9/9 . The loop provides you with the current object in the collection and it is incremented automatically.com proceedings.. Software Design Consultants.esri.E. they will devise their own standards and methodologies for their particular development goals. T.. As programmers work together.. Indianapolis. It is also better suited for handling objects. A basic understanding of OOP will enable programmers to realize the potential capabilities of MapObjects as a programming tool.com/library/userconf/proc97/proc97/to350/pap325/p325. standards enable teams of programmers to work efficiently together and encourage modularity and code reuse. By implementing standard development practices. programmers can take advantage of OOP constructs to build a robust application. IN.R. modularity. MapObjects is built upon an Object Oriented Programming paradigm. 20190 Telephone: (703)834-5000 Email: [email protected] strCtrlName = objCtrl. 1995. SAMS Publishing. Montlick. This method will always be at least as fast as the normal For. et al. the use of comments.. In addition. user interface issues and optimization methods are meant more as an introduction and starting point rather than an exhaustive listing.. Next loop when dealing with objects or collections. "Tricks of the Visual Basic 4 Gurus". J..06/03/14 Application Development with MapObjects and Visual Basic 4. naming conventions. The coding standards described above.
Report "Application Development with MapObjects and Visual Basic.pdf"