C Example

    next disabled - end of section

C# 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_cs.cs
namespace Peter.Toybox {     public class Ball    {         private string _Color;          private string _Motion;           public Ball(){             _Motion = "Still";          }          public string Color{             get {                 return _Color;              }              set {                 _Color = value;              }            }          public string  Motion {             get {                 return _Motion;              }          }          public void Roll() {             _Motion="Rolling";          }      }  } 

Enter this source example into your favorite text editor and save this as ball_cs.cs.

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 the Start button.

  2. Pick the Run option.

  3. Type cmd.

  4. Press Enter.

You should see a screen similar to Figure B.11.

Figure B.11. Typing cmd and pressing Enter in the Run dialog box takes you to a command prompt.
graphics/apbfig11.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 to where the files are stored, I type cd\websites\book\app_b.

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

Your drive and path will vary depending on where you saved the ball_class_cs.cs file. Now that I am in the same directory as the file, I can try to compile it. Type csc /t:library ball_cs.cs.

  • csc is the command to invoke the C# compiler.

  • /t:library tells it that I want a compiled library file, not an executable.

  • ball_cs.cs is the name of the source file.

If all goes well, your screen should now look something like Figure B.13.

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

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

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

In Figure B.14, 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_cs.dll file. You can check this by using the dir command. See the results in Figure B.15.

Figure B.15. 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/apbfig15.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 website. 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. Type bin and press Enter, as shown in Figures B.16 and B.17.

Figure B.16. Create a new folder.
graphics/apbfig16.gif
Figure B.17. Name the folder bin.
graphics/apbfig17.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. Under the View tab, select Show Hidden Files and Folders and uncheck Hide Protected Operating System Files. 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. You can see this in Figures B.18 and B.19

Figure B.18. Open the Folder Options dialog box.
graphics/apbfig18.gif
Figure B.19. Uncheck Hide Protected Operating System Files option.
graphics/apbfig19.gif

You should now be able to see ball_cs.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_cs.aspx
<%@ page language="c#" runat="server"%>  <%@ import namespace="Peter.Toybox"%>  <html>  <title>Run Ball Method</title>  <body>  <%  Ball objMyBall = 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 this as ball_class_cs.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.20.

Figure B.20. Success!! The ball class is working as expected.
graphics/apbfig20.gif

Can you see how easy it is to expand the function and classes that you can use in the .NET Framework? Being able to build and compile your own classes opens up a whole new world of expandability and addresses code reuse that has been a very difficult issue to deal with in the past.

And because the .NET Framework allows you to just drop newly compiled classes into the bin directory without stopping and starting web service in IIS, you can make additions and modifications to your objects without fear of how this might cause interruptions. You can change and recompile an object, drop it into the bin directory, and the next time a new call to the object is made, it will load that class into memory and use it. After all existing uses of the old object are dropped, that object will be completely removed from the server's memory and the new DLL will be used exclusively.


    next disabled - end of section
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