Creating libraries

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I would like to create a compiled library .net code which I can use in my
apps using imports and with references like mynamespace.myclass.mothod1.
What is the way to create such libraries?

From an organizational point of view, do I need to have one namespace in a
single file or can I spread it over several files?

Thanks

Regards
 
John said:
I would like to create a compiled library .net code which I can use
in my apps using imports and with references like
mynamespace.myclass.mothod1. What is the way to create such
libraries?

When creating a new project, choose "ClassLibrary" (not available in VB.Net
Standard).
From an organizational point of view, do I need to have one namespace
in a single file or can I spread it over several files?

You can spread it over several files or even several assemblies.
 
Thanks. Silly question but just for clarification, if I need to separate my
code into several areas like this;

mycompany.dataaccess. ...
mycompany.ui. ...
etc.

and each of these I would like as separate files (so I can import only the
ones I need), where do I make this distinction in my class library?

Thanks

Regards

PS: Let me know if it is too dumb a question and I need to read something.
:)
 
John said:
Thanks. Silly question but just for clarification, if I need to
separate my code into several areas like this;

mycompany.dataaccess. ...
mycompany.ui. ...
etc.

and each of these I would like as separate files (so I can import
only the ones I need), where do I make this distinction in my class
library?


There is no direct relation.

Namespace:
A file can contain one or several namespaces. Each Namespace can contain
further namespaces. The items (like classes) in the Namespaces can be spread
over several files or assemblies.

Import:
A simplification at file or project level to avoid typing the full qualified
name, e.g. "Form" instead of "System.Windows.Forms.Form"

Assembly/Library:
A collection of files.


Does this answer your question?
 
Thanks. Silly question but just for clarification, if I need to separate my
code into several areas like this;

mycompany.dataaccess. ...
mycompany.ui. ...
etc.

and each of these I would like as separate files (so I can import only the
ones I need), where do I make this distinction in my class library?

Just as an example, create a ClassLibrary. Right Click on the project and
select Project Properties. There is an item called Root Namespace. This
is commonly set to the Company name (e. g. MyCompany).

Then add one or more classes to the Library. At the top of the class, add
a namespace statement. For example if the class in question was for
fileio, you could do this:

'In FileIO.vb
Namespace FileIO
Public Class Class1
End Class

Public Class Class2
End Class
End Namespace


Then, in another project, you can add a reference to the class library and
add:

'In SomeOtherProject.vb
Imports MyCompany.FileIO

Private Sub SomeSub()
Dim o As New Class1
Dim p As New Class2

'...etc.
End Sub


Using the Namespace statement, you can add any number of sub levels. For
example, in another class you do this:

'In SomeOtherClass.vb
Namespace FileIO.Special
Public Class SpecialClass1
End Class
End Namespace

And even though that is in a different .dll, it would still be imported
like this:

Imports MyCompany.FileIO.Special

Public Sub AnotherSub()
Dim x As New SpecialClass1
End Sub

Look up the Namespace statement in the help and that should get you
started. I think it is good practice to set you root namespace to the
company name or some unique identifier.

Hope this helps
 
You have made it so easy for me, I created a quick class library project
with namespaces & classes and it worked!

It is sure great to see a library with your company name. :)

Thanks

Regards
 
Back
Top