Using Components in ASP.NET, Part 2By Tribikram Rath
In Part 1 we examined building and using components in classic ASP.
Also, we examined the benefits of using components with ASP.NET. In this final part we'll build our own
component in VB.NET, compile it, copy it to the
/bin directory, and use it through an ASP.NET
Building our .NET Component
To implement the
Math component in ASP.NET let us write it with the same capabilities
that were in the VB6 implementation of the component. The following codes shows how to write a Class Library
in .Net. The code below is in VB.NET syntax - you can use whatever text editor you like to build this component
(such as Notepad), but if you have a copy of Visual Studio .NET, you'll likely enjoy the rich features it has to offer.
Save this file with any suitable name say -
math.vb. If you used Visual Studio .NET, you can
compile the above code by simply going to the Build menu and building the project. If you used another text
editor, say Notepad, you must compile the code by hand through the command-line compiler. This is fairly simple.
This will create an assembly,
DOTNETMath.dll. At this point, you must copy the DLL to the
directory. Once you perform this copy, you can start using the component in your ASP.NET Web pages - no
registration or Web server restart needed! Generally, the assemblies from the
/bin folder are
automatically loaded by ASP.NET when the application is started. Optionally, you can add a
file for adding any other specific assembly in the current folder from the
/Bin folder by
using the following syntax:
Now, let us see how to use this assembly in an ASP.NET Web page. Recall that the namespace given in the
DOTNETMath. Hence, the first line of your ASP.NET Web page should be as follows:
This line of code imports the namespace into the ASP.NET Web page. This import directive simply allows you to
use the short-hand class notation to create an instance of the object (i.e.,
Operations); if you
omit the above line, you will need to refer to the component by both its namespace and class name
DOTNETMath.Operations). To get the new instance of the component we do:
Now, all the methods, properties, etc. can be used by taking the reference of the new instance of the class.
Suppose I want to use the
The Snap Shot to the right shows the component in action. Another point to be noted is that, whenever the assembly is loaded by the ASP.NET, that scope of the instance is only available to this application not to the others those are running parallel. Hence there will be no namespace conflict even another assembly with the same namespace is used by another application on the same machine. For more information about ASP.NET, be sure to check out ASP.NET.4GuysFromRolla.com.