473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple class library (dll) example?

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...

Nov 10 '07 #1
3 7255
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.

"kimiraikkonen" wrote:
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...

Nov 10 '07 #2
On Nov 10, 6:59 am, kimiraikkonen <kimiraikkone...@gmail.comwrote:
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.

--
Tom Shelton

Nov 10 '07 #3
On Nov 10, 6:28 pm, Tom Shelton <tom_shel...@comcast.netwrote:
On Nov 10, 6:59 am, kimiraikkonen <kimiraikkone...@gmail.comwrote:
Hi there,
I want to begin understanding howclasslibraries 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 perclasslibraryand call them under my executable
project?
Thanks...

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
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.

--
Tom Shelton
Very thanks Tom.
Dec 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Derrick | last post by:
I have the below simple class - public class MyClass { protected string m_stName; protected string m_stCode; protected int m_iId; public MyClass() {}
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
7
by: Mark Prenter | last post by:
Hi all, I'm fairly new to .NET and I haven't done much in C++ before, nothing complex anyway, but I have a pretty good understanding of programming in general. What I'm trying to do is create a...
2
by: AAguiar | last post by:
Thanks for your replies. Last week, I continued working with this problem. Trying to reproduce the error, I developed a short example and found that the problem is related to strong name dll. ...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
4
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: ...
4
by: Chris | last post by:
Hi this time I am interested in console applications only. Now I need to create a simple .dll. How to do that? I am totally newbie in dll creation so be patient ;) I have searched internet but I...
5
by: Rowan | last post by:
Hi there, I am planning/building an n-Tier system with 2 presentation layers (web, windows) which will share the BLL and DAL. This is my first experience with .Net. As a VB6er I had to work...
2
by: =?Utf-8?B?d3VyemVsQ2lkZXJtYWtlcg==?= | last post by:
I have created a very simple C# class library (DLL). I have ticked the "Register for COM interop" option in the project properties dialog. I have made the in the AssemblyInfo.cs file. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.