VBCOM TUTORIAL(2)[3]

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

本文简介:选择自 bgl218 的 blog

therefore, use the class_terminate method to cleanup (close files, destroy helper objects, etc.) before the server is gone for good. 

now for the public interface of cprogressvalue, show and hide.  firstly, the value property is not implemented using an integer variable, but instead is a logical property physically realized by appropriate get and let methods (look closely at the bottom pane of the browser window above).  logical properties offer many benefits:   validation, the ability to take action when a property changes, read-only data, etc.  however, a stronger motivation is that com simply does not allow a class to contain public data properties.  in a com-compatible class, the only public members may be methods. 

ignoring com for the moment, a logical value property makes sense in this situation since the server needs to update the progress indicator in response to a change in value.  the let method is used to capture a write to the logical property, much like the let statement in vba assigns a new value to a variable.  note that the new value is passed as a parameter to the method:

  public property let value(byval percentage as integer)  
    frm.pbarprogress.value = percentage '** update progress bar
    frm.lblpercentage.caption = _  
       cstr(percentage) & "%" '** update label
    frm.refresh '** redraw form
  end property

the new value is simply assigned to the underlying windows 95 progressbar control on the progress form, as well as written to a label.  in order for the client to be able to read the current progress value, a corresponding get method is also provided:

  public property get value() as integer
   '** return current state of progress bar
    value = frm.pbarprogress.value
  end property

get methods are actually functions which return the current value of the logical property. 

the final two members of cprogress are the show and hide methods.  these are perhaps the easiest of the bunch, since their function is obvious:

  public sub show()
    frm.show '** show form and redraw
    frm.refresh
  end sub
 
  public sub hide()
    frm.hide '** erase form but leave form object in memory
  end sub

that's it, the class cprogress is now completely implemented!


classes vs. instances

before we begin our discussion of com, let's review the notion of  classes, and instances of classes.  consider the following client code:

  dim p1 as cprogress, p2 as cprogress, p3 as cprogress
 
  set p1 = new cprogress
  set p2 = new cprogress
  set p3 = new cprogress
 
  p1.value = 40
  p2.value = 5
  p3.value = 99
 
  p1.show
  p2.show
  p2.hide
 
  stop '** pause client and activate the debugger

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

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

go top