When to use "imports" statement?

K

kimiraikkonen

Hello,
I want to ask about "imports" statement. Some projects must be
inserted with "imports xxxx" statements before beginning coding. But
how do i know when to use or do i have to use "imports" statement?


How will i know which "imports" will my project need? From books or
how can i guess?

Thanks...
 
L

Lloyd Sheen

kimiraikkonen said:
Hello,
I want to ask about "imports" statement. Some projects must be
inserted with "imports xxxx" statements before beginning coding. But
how do i know when to use or do i have to use "imports" statement?


How will i know which "imports" will my project need? From books or
how can i guess?

Thanks...

You use imports to bring in definitions. For example lets say you want to
use the methods provided within the System.IO namespace (for example
GetTempFileName).

Now without imports you could use the following:

Dim str as String = System.IO.Path.GetTempFileName()

With the imports
Imports System.IO

your code would be:

Dim str as String = Path.GetTempFileName()


Now there are lots of namespaces that hold identical interfaces.

There are both:

Microsoft.VisualBasic.MyServices.FileSystemProxy.GetTempFileName() As String
and
System.IO.Path.GetTempFileName() As String

to use the statement

Dim str as String = GetTempFileName()
you would add the correct imports statement and then you would have good
code.


This is a big subject but I hope what I have written helps.

Lloyd Sheen
 
K

kimiraikkonen

You use imports to bring in definitions. For example lets say you want to
use the methods provided within the System.IO namespace (for example
GetTempFileName).

Now without imports you could use the following:

Dim str as String = System.IO.Path.GetTempFileName()

With the imports
Imports System.IO

your code would be:

Dim str as String = Path.GetTempFileName()

Now there are lots of namespaces that hold identical interfaces.

There are both:

Microsoft.VisualBasic.MyServices.FileSystemProxy.GetTempFileName() As String
and
System.IO.Path.GetTempFileName() As String

to use the statement

Dim str as String = GetTempFileName()
you would add the correct imports statement and then you would have good
code.

This is a big subject but I hope what I have written helps.

Lloyd Sheen

Hi Mr. Sheen,
Thanks for your samples and explanation. As i understand, "imports" is
used to shorten syntax. It's a bit like "header files" in C++ i
suppose. And without imports statement, the same code function can be
coded within a line, like shown in your examples.

Thank you.

kimiraikkonen
 
G

Guest

You can also use Imports to abbreviate a namespace, such as
Imports F = System.Windows.Forms

or if you have your own project of custom user controls and other objects
called, for example, MyUserControls, and your exe project holds a reference
to that project or dll...In the exe's classes you could use...

Imports C = MyUserControls

Then when you type "C." you will see all of your custom controls and objects.
 
C

Cor Ligthert[MVP]

Kimi,

It has nothing to do with headers. There is no need to use it, however it
makes your program easier to write and easier to maintain.

Your main question however was in my idea where to find it. Look in MSDN and
see what namespace a class uses. Have then a look in your references (to
find in project and with the right click on the folder for that in your
project solution) if you have that already then you can use the import. If
not than select it. It area DLL's what stends in .Net for a Library. They
contains the classes which you can use.

The Imports says nothing more than that you have an abbrivation, it imports
nothing. In this case I find the C# equivalent Using much more fitting for
the purpose.

Cor
 
K

kimiraikkonen

Kimi,

It has nothing to do with headers. There is no need to use it, however it
makes your program easier to write and easier to maintain.

Your main question however was in my idea where to find it. Look in MSDN and
see what namespace a class uses. Have then a look in your references (to
find in project and with the right click on the folder for that in your
project solution) if you have that already then you can use the import. If
not than select it. It area DLL's what stends in .Net for a Library. They
contains the classes which you can use.

The Imports says nothing more than that you have an abbrivation, it imports
nothing. In this case I find the C# equivalent Using much more fitting for
the purpose.

Cor

Yes, "using" looks like "imports". Has some namespace decleractions.
Thanks.
 

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