Published: Wednesday, November 21, 2001
Using Components in ASP.NET, Part 2
By Tribikram Rath
Read Part 1
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
Web page!
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.
Imports System
Imports Microsoft.VisualBasic
Namespace DOTNETMath
Public Class Operations
Public Sub New()
MyBase.New()
End Sub
Public Function Add (A As Single, B As Single) As Single
Add = A + B
End Function
Public Function Subs (A As Single, B As Single) As Single
Subs = A - B
End Function
Public Function Mul (A As Single, B As Single) As Single
Mul = A * B
End Function
Public Function Div (A As Single, B As Single) As Single
If B = 0 then
Div = -1
Else
Div = A / B
End If
End Function
End Class
End Namespace
|
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.
vbc /t:library /out:DOTNETMath.dll math.vb
|
This will create an assembly, DOTNETMath.dll. At this point, you must copy the DLL to the /bin
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 Web.config
file for adding any other specific assembly in the current folder from the /Bin folder by
using the following syntax:
<Configuration>
<System.Web>
<Compilation>
<Assemblies>
<Add Assembly="DOTNETMath"/>
</Assemblies>
</Compilation>
</System.Web>
</Configuration>
|
Now, let us see how to use this assembly in an ASP.NET Web page. Recall that the namespace given in the
library was DOTNETMath. Hence, the first line of your ASP.NET Web page should be as follows:
<%@ Import Namespace = "DOTNETMath" %>
|
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
(i.e., DOTNETMath.Operations). To get the new instance of the component we do:
' Create an instance of the component
Dim MathComponent as Operations
MathComponent = New Operations()
|
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 Add and Subs methods:
Dim AddResult, SubResult as Single
AddResult = MathComponent.Add (CSng (FirstNum), CSng (SecondNum))
SubResult = MathComponent.Subs (CSng (FirstNum), CSng (SecondNum))
|
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.
Happy Programming!
By Tribikram Rath