This article is aimed primarily at users creating ASP.NET web pages without Visual Studio -- perhaps using only Notepad and having their code-behind inline on the same page -- but who want to create a DLL with some global (public) functions that they can reference from any page. It is also, perhaps, a simple introduction to what DLLs are and how to use them. This article deals with VB.NET, but the same principles apply for other .NET languages.
There is no reason why such a DLL cannot be incorporated into a Visual Studio project too of course, but anyone using that has more direct methods of creating DLLs. Once created, they are treated like any other DLL, i.e. they should be placed in the application's /bin folder and referenced in the usual manner. This is a good start towards building up and keeping a library of useful functions that can be used in all your applications. Furthermore, being pre-compiled, functions within the DLL should execute faster than having them simply included in your page's inline code.
Using the code
The ZIP file contains a small application, vbc.exe, that is in fact probably already on your computer. The one in the ZIP file is in windows/Microsoft.NET/Framework/v1.1.4322. DLLs so created can also be used in higher versions of the framework, subject to whatever compatibility issues exist between frameworks. This is to say, they'll usually work just fine! However, a .NET 2.0 version exists in the corresponding folder for that framework, so if you particularly want to target that instead, find and use it in place of the one in the ZIP file.
I found it helpful to create a new folder in a more accessible location, /inetpub/vbc/, and copied the file there. This is a command-line executable, so to run it you need to open a Command Prompt window and navigate to this folder. To ease this task, the ZIP file also contains a sample batch (BAT) file that will take you straight there. You may need to amend this to suit your needs; it's simple enough to work out. So, the steps are:
- Create a new folder in inetpub called "vbc"
- Copy the file vbc.exe into it
- Open a Command Prompt window and note the default folder it opens in
- Copy the file vbc.bat into that folder, having amended it if necessary
Before we can actually make use of all this, we need to create the source file for our DLL! We will create a small DLL containing a public function that will return a string greeting a name passed in when called. Open a new blank text file and save it to the same vbc folder as above with a .vb extension, say myfirstdll.vb. Then add the following:
Public Class MyClass1
Public Function Hello(ByVal strName As String) As String
If strName.Trim = "" Then
Return "Hello! Who the @!*# are you?"
Return "Hello " & strName & "!"
In fact, the
imports are not necessary in this example, but are there as an example of how to import classes, which you will need to do for more realistic examples than this. You can, of course, add as many functions as you wish. Now open your command prompt window and type in
vbc if you have copied and configured the batch file. Otherwise, navigate manually to the vbc folder. Next, type and enter:
vbc.exe /t:library /out:myfirstdll.dll /r:system.dll
Notes about this:
vbc.exe /t:library - always use
/out:myfirstdll.dll - /out: followed by the name of the DLL you want to create
/r:system.dll /r:system.data.dll - list of DLLs imported preceded by /r:
myfirstdll.vb - your source file
Note point 3: knowing which classes to import and which DLLs are needed here is a matter of experience. Common useful ones might be:
These can be referenced in your vbc call accordingly. If you may want to include Data.SqlClient in your list of imports to make your code easier, you should only reference /r:system.data.dll in your vbc call. There is no system.data.sqlclient.dll. Take a look in the windows/Microsoft.NET/Framework/v1.1.4322 folder where the original vbc.exe file resides; you can reference any DLL in there. You should now see myfirstdll.dll in your vbc folder. Copy this over to your application's /bin folder and it is ready for use. Making a call to the
Hello function is now a simple matter of referencing the DLL in your ASPX page. For example:
<%@ import Namespace="MyNameSpace1" %>
The output of this page will be a simple
Hello World! message. That's it! Happy coding. -Fred
- 16 July, 2007 -- Original version posted