473,785 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

"kimiraikko nen" 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.comwrot e:
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.cat egory.specificf unctionalgroup. So they might be, if my
company was ABC something like: ABC.Utilities.W ordInterop 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.WriteLi ne("=========== === Adding ==============" )
Console.WriteLi ne("4 + 3 = {0}", calc.Add(4, 3))
Console.WriteLi ne("6 + 6 = {0}", calc.Add(6, 6))
Console.WriteLi ne("-2 + 5 = {0}", calc.Add(-2, 5))
Console.WriteLi ne("=========== =============== ==========")
Console.WriteLi ne()
Console.WriteLi ne("========== = Subtracting ===========")
Console.WriteLi ne("2 - 2 = {0}", calc.Subtract(2 , 2))
Console.WriteLi ne("5 - 8 = {0}", calc.Subtract(5 , 8))
Console.WriteLi ne("100 - 3 = {0}", calc.Subtract(1 00, 3))
Console.WriteLi ne("=========== =============== ==========")
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...@co mcast.netwrote:
On Nov 10, 6:59 am, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
Hi there,
I want to begin understanding howclasslibrari es 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 perclasslibrary and call them under my executable
project?
Thanks...

You create aclasslibrarypr oject 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

PublicClassCalc ulator
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.cat egory.specificf unctionalgroup. So they might be, if my
company was ABC something like: ABC.Utilities.W ordInterop 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.WriteLi ne("=========== === Adding ==============" )
Console.WriteLi ne("4 + 3 = {0}", calc.Add(4, 3))
Console.WriteLi ne("6 + 6 = {0}", calc.Add(6, 6))
Console.WriteLi ne("-2 + 5 = {0}", calc.Add(-2, 5))
Console.WriteLi ne("=========== =============== ==========")
Console.WriteLi ne()
Console.WriteLi ne("========== = Subtracting ===========")
Console.WriteLi ne("2 - 2 = {0}", calc.Subtract(2 , 2))
Console.WriteLi ne("5 - 8 = {0}", calc.Subtract(5 , 8))
Console.WriteLi ne("100 - 3 = {0}", calc.Subtract(1 00, 3))
Console.WriteLi ne("=========== =============== ==========")
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
9493
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
4266
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
7
1353
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 .DLL that contains a lot of the functions and classes that I normally use. I've followed the examples from http://www.c-sharpcorner.com/2/pr12.asp which contains a pretty good example of creating a simple .DLL. Even though the example is in C#,...
2
2126
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. Following is the example to reproduce it. I have a C++ project (mixed dll, managed and unmanaged), with classes: EncodeDecode.cpp, EncodeDecode.h, DecoderRing.cpp as follows: ---------------------------
5
1999
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 like this is a bug or that .NET doesn't support what I trying to do. I hope that one that is is microsoft certified read this because this must be a bug.
4
2662
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: To keep it really easy let's say this is the code: function addition(int, int); int X;
4
31840
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 still do not understand few things. please tell me how to write a very simple dll containing only a function that writes a text on console. the text will be a paremetr of this function. how to use that .dll in other applications. any help will be...
5
1472
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 hard to gain an object oriented perspective. I am sure that will get stronger as I progress. I've spent quite a bit of time studying the framework. But now I find I am struggling with the simple things. I am in a small company and therefore...
2
2156
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 can call a public method in this library from VBScript. So far so good, now I'd like to have some very *simple* loggging capability.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.