My first object!
So far we have discussed objects as controls in VB (the Textbox and RichTextBox - you should be remembering these by now!)
In this example, we注释:re going to make a different kind of object. When you make a control, it has a user interface (that means you can visually see something on the screen, such as the textbox itself). Another kind of object is one that doesn注释:t have a user interface, and we call this a DLL (of Dynamic Link Library if you want to impress your friends).
To make one of these, click File, New Project and select ActiveX DLL:
This makes a new project with one empty class module in it. On the properties pane, make sure that the Instancing property is set to 5 - MultiUse. This allows us to compile the DLL and expose the class module to other programs (such as the object browser (This is starting to get complicated! (Espically with all these brackets :)))
We only want this example to be simple, so why don注释:t we base it around the cheesy Hello World project we talked about earlier on.
Lets add a new method called SayHello that pops up a message box saying Hello World! on it.
But before you rush off to start coding. let me show you a useful tool that VB has. It注释:s called the VB Class Builder. To open it, click Project Add Class module and select VB Class Builder:
You注释:ll probably get a message box telling you that the project contains a class module not generated by the Class Builder, but just ignore it and press ok.
We want to add a new method, so click on Class1 and select File, New, Method. Enter a name as SayHello and click OK. You should now see the item SayHello in the right hand pane (contact me if you don注释:t, or simply scream :)
As that注释:s all we want, click File, Exit and Yes to update the project.
As you will see, we didn注释:t achieve much there! Two lines of procedure code - not much for all that faffying around. But as we continue through this series, you will get a handle on the uses of the Class Builder.
We only want a message box to show up, nothing too serious. Add this line of code:
Msgbox "Hello World!"
The procedure should look like this:
Public Sub SayHello()
MsgBox "Hello World!"
End Sub
Time to build! Save the project, and a click File, Make Project1.DLL.
There you go! You注释:ve just complied your first object! (OK, so maybe this isn注释:t your first time, but who cares?)
Now all we need to do is test it. Lets add another project - File, Add Projcet, Standard EXE. (Uhh? How do we load up the object - it doesn注释:t appear under the components dialog box)
Good question (I dunno who asked it, but it was good :) Because a DLL is not a control, we need to make a reference to it (because we are not loading any kind of user interface)
So, click File, References and scroll down and select Project1:
(If it注释:s not there, click on the browse button and locate the Project1.DLL)
Click ok and your object is now loaded. Just to make sure, press F2 to load up the object browser and select Projec1 from the combo box. See the SayHello method there? Good.
Now lets do some cool coding and call the SayHello method:
- Add a declaration of the object:
Dim m_clsProject1 As Class1
- Now, under the Form_Load event, create the object:
Set m_clsProject1 = New Class1
And call the SayHello method:
m_clsProject1.SayHello
Run the project and see the message box appear! (If you get an error let me know and I will sort it out :)
Wow! Is that the time? I better be on my way for now, but next week I注释:ll come back to looking at what we have just done (creating objects and all that stuff) but for now, download the sample files:
Download the files!
And let me know what you thought of this article! (Remeber, click Post Feedback Now just below this text)
Have a nice week! (or was it Have a nice day? I注释:m losing my memory at the moment - more sleep required!)
C ya next week!
Sam
文章来源于领测软件测试网 https://www.ltesting.net/
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073