VBCOM TUTORIAL(2)[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 bgl218 的 blog

object-based programming

introduction

com programming is based on an object-oriented style of programming.  in vb, this means the use of classes and class-based object references.  if you are comfortable defining your own classes with logical properties and methods, and then using these classes to instantiate and manipulate objects, you can safely skip this section and continue the tutorial here.   otherwise, let's begin! 

we will study object-based programming in the context of an example:  a form-based progress indicator class called cprogress.   here's a picture of this class in action (you should see this for yourself by running the application \vbcom\demos\class-based progress indicator\app.exe;   if you have not already done so, you can download the labs from the setup page ):

oop-progress.gif

note the naming convention --- classes always being with a c.  it will be helpful if you also think of the c as standing for a concrete class, i.e. a class that can be turned into a run-time object that fulfills some function.

first, a bit of terminology.  a class is compile-time entity that a programmers writes.  in vb, a class is produced by adding a class module to your project.   a class may contain zero or more members, where a member is either a property (think of it as a variable) or a method (subroutine or function).  at run-time, you instantiate a class to produce an object that resides in memory.   in fact, you can instantiate the same class over and over again, each time producing a distinct object.  finally, to facilitate discussion, we will use the terms client and server to denote two objects that are interacting, in particular where the client object is accessing a property or invoking a method on the server object:

client_server.gif

using the class cprogress

the class is quite simple in design, having only one public property and two public methods:

  public value as integer '** percentage done
  public sub show() '** show the progress form
  public sub hide() '** hide the progress form

when this class is instantiated into an object, the value of the object represents the current status of the progress indicator, e.g. 40 (denoting 40%).  by changing this value, the client can show progress.  the object's methods, show and hide, are invoked by the client to draw and erase the progress indicator form, respectively.   altogether, this list of public properties and methods denotes the interface between clients and cprogress servers. 

at this point, let's look at some sample client code which instantiates and uses the cprogress class.  if you haven't already, open the supplied vb project \vbcom\demos\class-based progress indicator\app.vbp and view the code in the cmdprogress button of the main form:

  dim progress as cprogress '** object reference to
    '**  cprogress instance
  dim i as integer, _  
     j as variant  
 
  set progress = _  
     new cprogress '** new instance of cprogress

the first line declares an object reference variable (a pointer in c++ parlance) of type cprogress.  initially, an object reference variable points to no object, and thus has the value nothing.  the third line uses vb's new operator to instantiate an object of class cprogress, and then sets the object reference variable progress to this new instance.  at this point we have:

progress_instance.gif

the code continues by displaying the progress indicator form, and then executing a loop which simulates a long-running operation:

  progress.show '** invoke show method
 
  for i = 1 to 10
    progress.value = _  
      progress.value + 10 '** update by 10%
    for j = 1 to 1000000: next j '** pause to simulate activity
  next i

each time through the loop, progress increases by 10%.  finally, once the loop completes, the progress indicator form is erased from the screen and the progress variable reset to nothing:

  progress.hide '** invoke hide method
  set progress = nothing '** explicitly destroy instance

this last step is not strictly necessary, since vb will reset the variable to nothing automatically once it goes out of scope.  however, it is important to note that the number of references to an object determines the lifetime of that object, i.e. the duration it exists in memory.  by setting progress to nothing we are explicitly reducing the reference count of that object by 1.  once an object's count reaches 0, it is destroyed.  thus, in the example above, the object is immediately destroyed after the set statement is executed.


implementing the class cprogress

本文关键:vb,com,com+
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top