Advanced GISObjects/Interfaces Week 5 2008 Spring VB - IV
Objects - create your own • The object templates is called class modules • Object is an instance of a class • There are tangible objects, such as control on a form and intangible objects - used in a code, like a collection object. • A class defines properties, methods, events for all the objects created from that class. • Instances from same class have same set of properties but different values • Object variable is only a pointer. • Such as Map Class in ArcMap/ArcView. One map object points to world map with small scale and another points to Tennessee with larger scale and different projection.
Object Variables • Refer an object variable to existing tangible object use “Set” keyword. • Set lstMyListBox = frmMain.lstSlides • For Intangible Objects, object variables act as the “handles” for reaching the objects. To create an intangible object, declare a variable as the object class, then set this variable as New instance of the class. Dim MyCollection As Collection Set MyCollection = New Collection ‘point to new instance Dim YourCollection As Collectioin Set YourCollection = MyCollection ‘point to existing object variable Set MyCollection = Nothing ‘release an object variable from pointing to an object
Generic and specific object types • Object, Form and Control are generic object type. Specific forms and types of controls are called specific object types. • The Object class is the most generic, with two subclasses , Form and Control (frmMain, txtMyText) • You can create instances from any form in the project.- Create a new project with cmdCreate Private Sub cmdCreate_Click() Dim frmNew As frmMain Set frmNew = New frmMain frmNew.show End Sub ‘click cmdCreate 3 times, you will create 3 identical forms
Keyword “New” • Dim X As New Collection ‘ one-step approach, slow • Dim X As Collection ‘two-step approach, better • Set X = New Collection • Generic objects and controls can not be created with the New keyword • Set X = New Object • Set X = New Form • Set X = ListBox • Set X = New TextBox • Set X = New ListBox • Set X = New txtTitle • Set X = New lstSlides This is ok! Dim X As Control Dim Y As Control Set X = frmMain.lstSlides Set Y = X
Class – general structure • Public Class ClassName • ‘Properties definitions go here • ‘Property Get/Set statements go here • ‘Methods go here • End Class
Class Property • Default value for title (if a property has a default value) type in code in Class_Initialize() procedure Private Sub Class_Initialize() mvarTitle = “Default Title” End Sub • To create a read-only property, you don’t need to supply the Property Let procedure. For write-only property, no need for Get procedure • Read-only procedure: e.g. Count property of a class • Write-only property: user change password but doesn’t show existing password
Create a Class Option Explicit 'General Declaration section Private mvarTitle As String 'Write property Public Property Let Title(NewTitle As String) mvarTitle = NewTitle End Property 'Read property Public Property Get Title() As String Title = mvarTitle End Property • Project > Add Class Module. The name of the class module is the name of the class to be created. Class module is saved as .cls files. A .cls file can be added to any VB project, where instances of the class can be created. You may rename it as “DVD” • Create a Title property for DVD • Type in codes as in the box • mvarTitle is used to store the property value within the class module • Suppose you have a class named DVD, and an instance named aDVD, then create a form to set title of DVD to whatever user’s inputbox provides, and then show it to the txtDVD box.
Create class DVD with properties and methods • In “Insert | Class Module” you will have chance to insert a class to your project. • Type in the codes from previous slide • Add a method “PlayMusic” with variable receiving value from calling sub. Then the value would be determined by Case 1,2,3,4 and its corresponding music type will be shown using msgbox. • In the form of the calling sub, prepare a cboSelection with values (1,2,3,4) initialized by form_initialize event. • In the same form, use a cmd to pass value selected in cbo to aDVD (object of DVD class) and then show message.
Create methods • Simply create a procedure or function within Class code window Public Sub Welcome() MsgBox "Welcomes to the slide show!" End Sub Public Sub SpecialWelcome(strName As String) MsgBox strName & ", welcome to the slide show!" End Sub • Call methods from frmMain>cmdClick() Dim CallClass As Class1 Set CallClass = New Class1 CallClass.Welcome CallClass.SpecialWelcome “Peter”
clsMyFirstClass –Create a class in your project 'General Declaration section Private mvarTitle As String 'Write property Public Property Let Title(NewTitle As String) mvarTitle = NewTitle End Property 'Read property Public Property Get Title() As String Title = mvarTitle End Property ‘Method Public Sub Welcome(strMsg As String) MsgBox strMsg End Sub
Instance of Object (Form with cmdCall control) Private Sub cmdCall_Click() Dim myObject As Object Set myObject = New objFirst Dim mTitle As String Dim nTitle As String mTitle = InputBox("Type in title here") 'Set myObject's title as mTitle myObject.Title = mTitle 'Get title from myObject nTitle = myObject.Title 'Show title of myObject using MsgBox MsgBox "The title of object is " & nTitle myObject.Welcome "Hello World, Hello World" End Sub
Human IGreeting Handshake Smile IAnimal WagTail Name Name Bark Eat Eat Catch Frisbee Breathe Breathe Interface - part of Component Object Model(COM) • A logical group of methods and properties for a class. It may be based on the level of generality or on some other similarity of use or purpose. For example, Human and Dog (by convention, interface names start with the letter “I,” shown w/ lollipops) Dog IBehavior IAnimal Both Human and Dog class have IAnimal interface. The IBehavior in Dog and IGreeting in Human are unique to themselves. You want to set a Human or Dog’s name, you need to go through the IAnimal interface
Procedure for accessing methods/properties of components • To access the methods/properties of components • 1) Declare an object as the interface (Dim pHuman As IGreeting) • 2) point the object variable to a new instance of the component (Set pHuman = New Human) • 3) Access the methods/properties (pHuman.HandShake, pHuman.Smile) • 4) you can only access those properties/methods supported by the declared interface. If you want to access other methods/properties, you need to use the QueryInterface process.
Human IGreeting Handshake Smile IAnimal Name Eat Breathe QueryInterface • Allows an object variable of one interface to access properties/methods of other interfaces of the same component. Dim pHuman As IGreeting Set pHuman = New Human pHuman.HandShake pHuman.Smile • ‘Using QueryInterface to access properties and methods of the IAnimal interface Dim pAnimal As IAnimal Set pAnimal = pHuman pAnimal.Eat pAnimal.Breathe or do the error-checking first If TypeOf pHuman Is IAnimal Then point the variable to the object variable of other interface
QueryInterface – examples from book and arcobjectonline.esri.com, (g:/classes/4850/w_arcObjectStartSlides.pdf)
Switching Interfaces from one to another One dog only…………….. ‘Create a dog and Set Color Dim pPet as IPet Set pPet = New pDog pPet.Color = “Golden” pPet.Name = “Rover”
Interfaceless Class and Interfaced Class • 1)Choose appropriate Interface to work with, such as Name property in IMap interface of Map. • 2) Declare the variable as IMap instead of Map class, but still set to New Map. Dim pMap as IMap Set pMap = New Map pMap.Name = “My Map” ‘Accessing Extent property, then a new Interface will be used. Dim pActiveView As IActiveView Set pActiveView = New Map pActiveView.Extent = someNewExent Dim e As Elephant Set e = New Elephant E.Name = “Big Ears” To view diagram - Open it from C:\Program Files\ArcGIS\DeveloperKit\Diagrams
Using IApplication and IDocument • ArcMap starts, two objects are in use – Application and MxDocument. The name of Application object variable is “Application” and the name of the MxDocument object variable is “ThisDocument” (all begin with p). Application IApplication Caption:String Name: String Two-sided barbell: get and set values Left-sided barbell: get value only Arrow: method PrintPreview
Another Example of QI Dim pPoint As IPoint Set pPoint = New Point pPoint.X = 100 ‘You may use pPoint.PutCoords 100,100 pPoint.Y = 100 Dim pTopoOptr As ITopologicalOperator Set pTopoOptr = pPoint Dim pBufferGeo As IPolygon Set pBufferGeo = pTopoOptr.Buffer(20)