Write your own components in your own hands

Source: Internet
Author: User
In the previous section, we learned about the use of components, although there are many components on the web, but often a small component will you pull out the U.S. dollar, and the domestic components are too few, want to pay the yuan has not been good to dig, forget, you simply write it, in fact, writing components is not difficult, nothing but is just beginning to write low-level components, Slowly began to write high-level components. To write ActiveX Server components, there are a variety of tools to choose from: Visual Basic, C + +, Java, and so on, here to choose the simplest VB6.0, as long as you have used VB, package you less than 1 hours, to do a group out.

One, the component writes the quick--writes the component the most basic step
This section explains the basic steps of writing components, and if you have a desire to learn further, refer to the "Create ActiveX DLL" section in the VB6.0 Help document (the MSDN library Chinese file name is Vbcon98.chm).
Objective: To write a simplest adder component that has two properties, one method. We start with the most familiar place, which is to assume that the component already exists, and look at the code that uses the component in asp:
<%
Dim Objsum
' Create the component first, the component is identified as Testdll.sum
Set objsum = Server.CreateObject ("Testdll.sum")
' Remember the "Testdll" in Testdll.sum, "Sum".
Objsum.augend = 10.52 ' Augend property is Summand
Objsum.addend = 382.41 ' Addend property is Addends
result = Objsum.plus ' Plus method sum
Response.Write Result
Set objsum = Nothing
%>
Be sure to keep these keywords in mind: Testdll, Sum, Augend, Addend, Plus, which will be closely related to the work that follows. Here are the detailed steps:
1. Run VB6.0, on the File menu, click New Project → In the New Project dialog box, double-click the ActiveX DLL icon →visual Basic will automatically add a class module to the new project CLASS1 (as you can see in the Project Explorer). As shown in Figure 1).
2. Press the F4 key to open the Properties window. Double-click the Name property to change "Class1" to Sum.
3. On the Project menu, click Project 1 properties to open the Project Properties dialog box. Fill in the Testdll in the project name and fill in the "Create an ActiveX DLL sample" in "Engineering description." As shown in Figure 2.
Hint: ASP create the identity in the component testdll.sum the meaning of these two items is clear.
4. On the File menu, click Save Project to save the project file by using the following name: Setupdll.cls and Setupdll.vbp.
5. To create a property for the Sum class:
(1) On the Tools menu, select Add procedure to open the Add Procedure dialog box. In the Name box, enter Augend, click Properties, and then click OK, as shown in Figure 3 (note: The current focus should be in the code window or the Add procedure menu is not valid).
(2) On the Tools menu, select Add procedure to open the Add Procedure dialog box. In the Name box, enter Addend, click Properties, and then click OK.
6. To create a method for the Sum class:
On the Tools menu, click Add Procedure to open the Add Procedure dialog box. In the Name box, enter Plus, in the type, you can select "subroutine" or "function", in this case, because you want to return addends and the sum of the addends, click Functions, and then click OK.
Now, the previous ASP code in the five key words have been used, this is a lot of things understand it.
7. All the code in the Class module sum Code window is as follows:
Option Explicit
Private Mdbl_augend as Double
Private Mdbl_addend as Double

Public Property Get Augend () as Double
Augend = Mdbl_augend
End Property

Public Property Let Augend (ByVal Vnewvalue as Double)
Mdbl_augend = Vnewvalue
End Property

Public Property Get Addend () as Double
Addend = Mdbl_addend
End Property

Public Property Let Addend (ByVal Vnewvalue as Double)
Mdbl_addend = Vnewvalue
End Property

Public Function Plus ()
Plus = Augend + addend
End Function
You've noticed that the above code is a little different from the system-generated code, mainly because the public Property Get Augend () As Variant is replaced with the actual data type double.
Note: In fact, when the x = Objsum.augend statement is executed, the property gets procedure is invoked to get the Mdbl_augend value, and the property Let procedure is invoked when the Objsum.augend = 10.52 statement is executed, and the 1 0.52 assigned to Mdbl_augend.
8. In the final step, on the File menu, click Build. Dll (K) ... ", Generate file SetupDll.dll.

Here, the component has been created. Now, we can write a test project in VB6 to test whether the file is correct or not.
1. On the File menu, click New project → double-click Standard EXE.
2. On the Project menu, click reference → press the browse button in the Reference dialog box to select the SetupDll.dll file you just compiled → Click OK.
3. Finally, write the following code in the Code window:
Option Explicit
Private Sub Form_Load ()
Dim Objsum as Testdll.sum
Set objsum = New Sum

Objsum.augend = 10.52
Objsum.addend = 382.41
MsgBox Objsum.plus
End Sub
Run it and the results are correct, and we'll use this component in the ASP. Copy the SetupDll.dll to the Web server, register the component with the "Regsvr32.exe setupdll.dll" command, and then execute the ASP program at the beginning of this section in the browser. If it doesn't work, you can try packing with VB6, then install, the component will register automatically, and some of the necessary VB6 run files will be installed automatically.
Description: All programs in this section are in the setupdll.zip of the download package.
In the example above, we use the property procedure properties get and let to add properties (property to read the value of the property, and the property let to assign values), if you need to verify the value of the set, you can The let process writes the appropriate code. Like the following code:
Public Property Let Augend (ByVal Vnewvalue as Double)
If Vnewvalue < Then
MsgBox "What the hell! A value less than 100, you can do it in your head.
Else
Mdbl_augend = Vnewvalue
End If
End Property

Other ways to add attributes and methods to a component
1. The simplest way to add a property is to add a public variable, such as the simplest code in the previous example:
Option Explicit
Public augend as Double ' Add Augend property
Public addend as Double ' Add Addend property

Public Function Plus () as Double
Plus = Augend + addend
End Function
However, the method can only set a read/write property and cannot validate the assigned value. If you only need to add a read-only property: In the previous example, delete the public properties let Augend (ByVal vnewvalue as Double) procedure, Augend becomes a read-only property and cannot be assigned a value. Of course, to add a write-only property, simply delete the properties get procedure.

2. Using the Class Builder utility
VB6 also has a tool for adding properties and methods: On the Add-Ins menu, click Add-in Manager, select VB Class Builder Utility in the Pop-up Add-in Manager dialog box, and select Load/unload in the load behavior (Figure 4). Then, on the add-in menu, click Class Builder utility, which you will use as soon as you see it.

Let me tell you a few things to pay attention to
1. The above example is a simple can not be a simple component, more practical components generally have at least one module (module).
2. If you are not careful, Visual Basic can produce a single-threaded component.
Perhaps the most common pitfall is the use of components that are not designed to run under ASP, such as "single-threaded" components. Therefore, make sure that the thread module is set to unit thread on the General tab of the project Properties page.
3. About the "Type Mismatch" error.
A good suggestion is that it is best to declare the out parameter to be a Variant, and note that this setting is not in the example above.
4. About the use of global variables.
Try to avoid using global variables in your components. In Visual Basic terminology, this is indicated in the standard. There are no public or Global variables in the BAS module. Because global variables are not really global. Each thread has its own copy, and if several methods execute exactly on the same thread, they see the same variable; otherwise they are accessing different copies of those variables. This means that you may have assigned a value to a global variable (in thread A), but another user (executing in thread B) does not see the new value. The reason for this is that Visual Basic uses thread local storage (TLS) To reference global variables internally. This means that each thread has a copy of its own public variable, and because it has multiple replicas, global data is not truly "global". That is, users who are running on the same thread will only be able to access the same variable, whether they expect it or not.

Finally, incidentally, VB6.0 introduces a new Visual Basic application: the IIS application. You can create an IIS application like an ActiveX DLL, any application created with Active Server Pages can be created in the VB development environment, and you can completely replace all Active Server Pages with a single VB application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.