This section takes the ball class from Chapter 2 and compiles it as an object that will be available to the ASP.NET application.
Here is the ball class.
Namespace Peter.Toybox Public Class Ball Private _Color as String Private _Motion as String Public Sub New() _Motion = "Still" End Sub Public Property Color as String Get Return _Color End Get Set _Color = value End Set End Property Public ReadOnly Property Motion as String Get Return _Motion End Get End Property Public Sub Roll() _Motion="Rolling" End Sub End Class End Namespace
Enter this source example into your favorite text editor and save this as ball_vb.vb.
After you have the file saved, you can try to compile it. On my machine, the files are saved in d:\websites\book\app_b\. If your files are stored in a different location, the exact sequence of commands you need to issue will be slightly different. Substitute your path as appropriate.
Click on the Start button.
Pick the Run option.
Type cmd.
Press Enter. You should see a screen similar to Figure B.1.
The section "C:\Documents and Settings\Administrator>" reflects the current directory. It is separated into two parts: the "C:" and the "\Documents and Settings\Administrator" portion. The "C:" is the drive letter, and the "\Documents and Settings\Administrator" is the path. My files are stored on a different drive. I can go to that by typing D:. This places me in the root of the D drive. To change where the files are stored, I type cd\websites\book\app_b (see Figure B.2).
Your drive and path will vary depending on where you saved the ball_vb.vb file. Now that I am in the same directory as the file, I can try to compile it. Type vbc /t:library ball_vb.vb.
vbc is the command to invoke the Visual Basic compiler.
/t:library tells it that I want a compiled library file, not an executable.
ball_vb.vb is the name of the source file.
If all goes well, your screen should now look something like Figure B.3.
However, if you made any mistakes, you may receive an error message, like that in Figure B.4.
In Figure B.4, I misspelled Namespace. The program doesn't know what to make of it and reports that in the first block. Then, because the namespace wasn't declared properly, it notifies you that there is a closing namespace without an opening one. Both these problems are remedied by simply fixing the typo and issuing the command again.
When you do succeed in compiling your file, the result is a ball_vb.dll file (see Figure B.5). You can check this by using the dir command.
Unfortunately, this file doesn't do much good out here. If you want to use this class in a web application, you need to copy the file into a special directory on the web server called bin. Chances are, this directory doesn't exist yet if this is the first time you've compiled a component.
Using Explorer, navigate to the root of your web site. If you do not see a directory named bin, right-click in the window and select the New option. A submenu should appear asking what you would like to create. Choose Folder. The menu goes away, a new folder is created, and you should be able to type in the name of the folder (see Figure B.6). Type bin and press Enter (see Figure B.7).
Now, again using Explorer, go back to where you saved the source file and copy the dll there to the new bin directory you have just created.
Note |
Windows considers .dll files to be system files. Depending on your settings, you might not see a dll file where you compiled it. If this is the case, go to the Tools menu of the folder and choose Folder Options (see Figure B.8). Under the View tab, select Show Hidden Files and Folders and uncheck Hide Protected Operating System Files (see Figure B.9). You may also find it helpful to uncheck the option above the Hide Protected Files option, called Hide File Extensions for Known File Types, as well. With this option checked, the files' extensions appear in Explorer so that it will be easier to identify the different file types you are using. Figure B.8. Open the Folder Options dialog box.Figure B.9. Uncheck the Hide Protected Operating System Files and click OK. |
You should now be able to see ball_vb.dll in the directory. Copy this file to the bin directory you created. Now you need a web page to test the component out. Create a page, and enter in the following code:
<%@ page language="vb" runat="server"%> <%@ import namespace="Peter.Toybox"%> <html> <title>Run Ball Method</title> <body> <% dim objMyBall as New Ball() Response.Write("<u>Before Roll Method</u><br>") Response.Write("MyBall: " & objMyBall.Motion & "<br><br>") objMyBall.Roll() Response.Write("<u>After Roll Method</u><br>") Response.Write("MyBall: " & objMyBall.Motion) %> </body> </html>
Save the page as ball_class_vb.aspx, and then browse to it at the location of your application. The example application happens to reside at a local IP of 192.168.1.53. Your file will most likely reside somewhere within the http:// localhost application. See the results in Figure B.10.
Top |