Visual Basic .NET Example

   

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.

ball_vb.vb
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.

  1. Click on the Start button.

  2. Pick the Run option.

  3. Type cmd.

  4. Press Enter. You should see a screen similar to Figure B.1.

    Figure B.1. Typing cmd and pressing Enter in the Run dialog box takes you to a command prompt.
    graphics/apbfig01.gif

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).

Figure B.2. Change to the directory where your class file is located.
graphics/apbfig02.gif

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.

Figure B.3. If all goes well, you will return back to the command prompt without any errors.
graphics/apbfig03.gif

However, if you made any mistakes, you may receive an error message, like that in Figure B.4.

Figure B.4. If your file generates errors during compilation, you will receive a detail of the error.
graphics/apbfig04.gif

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.

Figure B.5. When your compilation is successful, the result is a file with a .dll extension with an identical name as your .vb file. This is your component.
graphics/apbfig05.gif

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).

Figure B.6. Create a new folder.
graphics/apbfig06.gif
Figure B.7. Call the new folder bin.
graphics/apbfig07.gif

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.
graphics/apbfig08.gif
Figure B.9. Uncheck the Hide Protected Operating System Files and click OK.
graphics/apbfig09.gif

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:

ball_class_vb.aspx
<%@ 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.

Figure B.10. Success!! The ball class is working as expected.
graphics/apbfig10.gif


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net