|
Custom Libraries
|
|
|
If you cannot find a function you want, you can create one and be able to use it.
If you plan to use such a function over and over again in different programs, you can
create a library and store the function in it. You can even create one or a series of commercial functions
and be able to distribute or
sell it through a library.
|
A library is a program that contains procedures and/or other
resources that other programs can use. Such a program is created with the same
approach as the programs we have created so far. Because a library is not an
executable, it does not need the Main() procedure. A library usually has the
extension .dll.
A library can be made of a single file or as many
files as necessary. A file that is part of a library can contain one or
more procedures (or classes). Each procedure (or class) should implement a behavior that can eventually be
useful and accessible to other procedures (or classes). The contents of a library
are created exactly like those we have used so far. Everything depends on how
you compile it. To create a library:
- On the main menu of Microsoft Visual Studio, you can click File -> New ->
Project
- On the Start Page, on the right side of Create, you can click Project
- On the Standard toolbar, you can click the New Project button or click the arrow
of the New Project button and click New Project
In the Templates list of the New Project dialog box, click
Class Library. Accept or specify the name and click OK. Once the starting
project has been generated, write the necessary code. To compile it, on the main
menu, click Build -> Build Solution. If you were working from the command
prompt, you would execute the following command: vbc /target:library NameOfFile.vb
After doing this, a library with the name
of the file and the extension .dll would be created. If you use the above
technique, the new library would be created using the name of the file. If you
are working from the Command Prompt, to compile the project and create a custom name,
use the following syntax:
vbc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.vb
Here is an example:
vbc /target:library /out:Arithmetic.dll exo.vb
Practical
Learning: Creating a Library |
|
- Start Microsoft Visual Studio
- To start a new project, on the main menu, click File -> New ->
Project...
- In the Project Types list, make sure Visual Basic is selected.
In the Templates List, click Class Library
- Change the Name to Arithmetic and click OK
- In the Solution Explorer, right-click Module.vb and click Rename
- Type Operations.vb and press Enter
- Change the file as follows:
Public Class Arithmetic
Function Add(ByVal x As Double, ByVal y As Double) As Double
Return x + y
End Function
Function Subtract(ByVal x As Double, ByVal y As Double) As Double
Return x - y
End Function
Function Multiply(ByVal x As Double, ByVal y As Double) As Double
Return x * y
End Function
Function Divide(ByVal x As Double, ByVal y As Double) As Double
If y = 0 Then Return 0
Return x / y
End Function
End Class
|
- To create the library, on the main menu, click Build -> Build
Arithmetic
- To start another project, on the main menu, click File -> New ->
Project...
- In the Templates List, click Console Application
- Change the Name to Algebra and click OK
- In Windows Explorer or My Documents, locate the Arithmetic folder that
contains the library project. Access a sub-folder of the same name, followed
by the bin sub-folder, and followed by the Debug sub-folder
- Right-click Arithmetic.dll and click Copy
- Still in Windows Explorer or My Documents, locate the Algebra folder that
contains the current project. Right-click the sub-folder of the same name
and click Paste
- In the Solution Explorer, right-click Algebra and click Add Reference...
- In the Add Reference dialog box, click the Browse tab
- Use the Look In combo box to locate and select a sub-folder named Algebra
in the current project
- Click Arithmetic.dll and click OK
- In the Solution Explorer, right-click Module1.vb and click Rename
- Type Exercise.vb and press Enter. Accept to make all necessary
changes
- Change the the file as follows:
Imports Operations
Module Exercise
Public Function Main() As Integer
Dim Result As Double
Dim Oper As Arithmetic
Dim Number1 As Double, Number2 As Double
Number1 = 244.58
Number2 = 5082.88
Oper = New Arithmetic
Result = Oper.Add(Number1, Number2)
MsgBox(Number1 & " + " & Number2 & " = " & Result)
Return 0
End Function
End Module
|
- To execute the project, on the main menu, click Debug -> Start Without
Debugging
|
|