Simple class library (dll) example?

K

kimiraikkonen

Hi there,
I want to begin understanding how class libraries are written under
VB.NET and how can i call them under my executable project. For
example think an arithmetic calculator includes only plus, minus,
division, multiplying functions.

Yes it may not be necessary but how can i seperate each arithmetic
funtion into per class library and call them under my executable
project?

Thanks...
 
G

Guest

You will add a second project to your solution by right-clicking on the
solution, then choosing "add->new project". Choose Class Library. You will
add classes to this library as you require. The coding of the classes is no
different than if it were in the executable project. From your executable,
you add a reference to the project by normal means. The project tab will
list the other projects that you may add as a reference.
 
T

Tom Shelton

Hi there,
I want to begin understanding how class libraries are written under
VB.NET and how can i call them under my executable project. For
example think an arithmetic calculator includes only plus, minus,
division, multiplying functions.

Yes it may not be necessary but how can i seperate each arithmetic
funtion into per class library and call them under my executable
project?

Thanks...

You create a class library project containing your classes. Make the
classes you want to expose, public as well as the methods you want to
be visible out side of the assembly.

For example the class we want to expose in the DLL:

Option Strict On
Option Explicit On

Public Class Calculator
Public Function Add(ByVal first As Integer, ByVal second As
Integer) As Integer
Return first + second
End Function

Public Function Subtract(ByVal first As Integer, ByVal second As
Integer) As Integer
Return first - second
End Function
End Class


Now,in the project we want to use this class we need to make a
reference to the dll. This can be done in several ways - but in this
case, I'm going to use what's called a project reference. I'm going
to go to the solution explorer right click on the solution -> add ->
Existing Project, and select my dll's project file. Once it's added
to the solution, I'm going to go to the solution explorer and right
click on the application I'm going to use the object from and select
add reference. Once the add reference dialog comes up I'm going to
select the projects tab and double click on my dll project.

Once I do that, I can now add the imports statement - which will be
for the namespace i put my object in, in this case I let it default,
but in practice you should probably come up with a standardized way of
doing your namespaces. I tend to do something like this
companyname.category.specificfunctionalgroup. So they might be, if my
company was ABC something like: ABC.Utilities.WordInterop Anway, this
is the statement I add to my console app:

Option Strict On
Option Explicit On

Imports ClassLibrary1 ' this is the namespace containing my caculator
object

Module Module1

Sub Main()

End Sub

End Module
From then on we can just use it:
Option Strict On
Option Explicit On

Imports ClassLibrary1

Module Module1

Sub Main()
Dim calc As New Calculator

Console.WriteLine("============== Adding ==============")
Console.WriteLine("4 + 3 = {0}", calc.Add(4, 3))
Console.WriteLine("6 + 6 = {0}", calc.Add(6, 6))
Console.WriteLine("-2 + 5 = {0}", calc.Add(-2, 5))
Console.WriteLine("====================================")
Console.WriteLine()
Console.WriteLine("=========== Subtracting ===========")
Console.WriteLine("2 - 2 = {0}", calc.Subtract(2, 2))
Console.WriteLine("5 - 8 = {0}", calc.Subtract(5, 8))
Console.WriteLine("100 - 3 = {0}", calc.Subtract(100, 3))
Console.WriteLine("====================================")
End Sub

End Module

Anyway - I hope that helps. You can also, use the browse tab and
browse to the actual compiled dll. But, that can cause path issues
especially in team development. Though, project references aren't
always practicle either. The other way to reference the object is to
put it in the gac - which means strong nameing the assembly. And then
you will see it on the .NET references tab.
 
K

kimiraikkonen

You create aclasslibraryproject containing your classes. Make the
classes you want to expose, public as well as the methods you want to
be visible out side of the assembly.

For example theclasswe want to expose in the DLL:

Option Strict On
Option Explicit On

PublicClassCalculator
Public Function Add(ByVal first As Integer, ByVal second As
Integer) As Integer
Return first + second
End Function

Public Function Subtract(ByVal first As Integer, ByVal second As
Integer) As Integer
Return first - second
End Function
EndClass

Now,in the project we want to use thisclasswe need to make a
reference to the dll. This can be done in several ways - but in this
case, I'm going to use what's called a project reference. I'm going
to go to the solution explorer right click on the solution -> add ->
Existing Project, and select my dll's project file. Once it's added
to the solution, I'm going to go to the solution explorer and right
click on the application I'm going to use the object from and select
add reference. Once the add reference dialog comes up I'm going to
select the projects tab and double click on my dll project.

Once I do that, I can now add the imports statement - which will be
for the namespace i put my object in, in this case I let it default,
but in practice you should probably come up with a standardized way of
doing your namespaces. I tend to do something like this
companyname.category.specificfunctionalgroup. So they might be, if my
company was ABC something like: ABC.Utilities.WordInterop Anway, this
is the statement I add to my console app:

Option Strict On
Option Explicit On

Imports ClassLibrary1 ' this is the namespace containing my caculator
object

Module Module1

Sub Main()

End Sub

End Module


Option Strict On
Option Explicit On

Imports ClassLibrary1

Module Module1

Sub Main()
Dim calc As New Calculator

Console.WriteLine("============== Adding ==============")
Console.WriteLine("4 + 3 = {0}", calc.Add(4, 3))
Console.WriteLine("6 + 6 = {0}", calc.Add(6, 6))
Console.WriteLine("-2 + 5 = {0}", calc.Add(-2, 5))
Console.WriteLine("====================================")
Console.WriteLine()
Console.WriteLine("=========== Subtracting ===========")
Console.WriteLine("2 - 2 = {0}", calc.Subtract(2, 2))
Console.WriteLine("5 - 8 = {0}", calc.Subtract(5, 8))
Console.WriteLine("100 - 3 = {0}", calc.Subtract(100, 3))
Console.WriteLine("====================================")
End Sub

End Module

Anyway - I hope that helps. You can also, use the browse tab and
browse to the actual compiled dll. But, that can cause path issues
especially in team development. Though, project references aren't
always practicle either. The other way to reference the object is to
put it in the gac - which means strong nameing the assembly. And then
you will see it on the .NET references tab.

Very thanks Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top