By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

When To Use Imports And When To Use The Full Namespace Path ?

P: n/a
Hi ,

I have an assembly that hold few class and few imports which are not used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?
Best Regards ,

Tiraman :-)
Nov 20 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
On Tue, 25 May 2004 15:30:58 +0200, Tiraman wrote:

so my question is when to use the "Imports XXX"

And when to use this kind of statement


The imports statement is just there to save you some typing. I don't
believe it affects performance at all.

I think that you should use the Imports statement to save some time, but
only if it does not make the code ambiguous. Use full or partial
namespaces when it makes the code clearer. Mostly, I think, it is a matter
of personal taste. Others may have alternate opinions.

--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #2

P: n/a
Tiraman,
And in which way the performance will be better ?
Imports is a developer productivity aid, it does not impact runtime
performance at all.

So when to use Imports & when not, depends entirely on you.

Hope this helps
Jay

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:uN**************@TK2MSFTNGP11.phx.gbl... Hi ,

I have an assembly that hold few class and few imports which are not used in all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?
Best Regards ,

Tiraman :-)

Nov 20 '05 #3

P: n/a
Hi Tiraman,

In some casses it is better not to use the import.

The import imports the namespace in your IDE, that means that if it is a
namespace with a bunch of properties, interfaces whatever your IDE get
totally locked.

My example is MSHTML which make the IDE absolute unusable when an import is
set.

For that it is better to use the Full namespace, (which is also terrible
slow to set for that).

However this are exceptions I assume.

Cor
Nov 20 '05 #4

P: n/a
* "Tiraman" <ti*****@netvision.net.il> scripsit:
I have an assembly that hold few class and few imports which are not used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....


In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

P: n/a
In article <2h************@uni-berlin.de>, Herfried K. Wagner [MVP] wrote:
* "Tiraman" <ti*****@netvision.net.il> scripsit:
I have an assembly that hold few class and few imports which are not used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....


In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...


That's funny... I do mine by the length... In other words:

Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
....

And I import everything as well (though, in my case, since I am almost
always using C#, it's actually using but same diff :)

--
Tom Shelton [MVP]
Nov 20 '05 #6

P: n/a
That shows that you never used Mshtml

Cor
Nov 20 '05 #7

P: n/a
Tom,

* Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> scripsit:
so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....


In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...


That's funny... I do mine by the length... In other words:


That's what I did for a long time, but then I sorted them
alphabetically. It's just a matter of preference, but alphabetical
order makes finding imports easier, especially if there are > 10
imports.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8

P: n/a
"Tiraman" <ti*****@netvision.net.il> wrote in message news:<uN**************@TK2MSFTNGP11.phx.gbl>...
so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?


To me, it depends on how much I'm using a namespace.

Am I just using Math.Round() in one or two places? Then I'll type out
System.Math.Round - no big deal. Am I doing an intense printing job?
Then I'll Import System.Drawing.Printing. (And sometimes I do both,
on odd occasions when I want to be clear to a future developer where
something comes from.)

John Fiala
jcfiala523-at-hotmail.com
http://www.livejournal.com/users/fiala_tech/
Nov 20 '05 #9

P: n/a
Herfried,
I normally do mine in order of importance, where order of importance does
not matter I use alphabetically.

Order of importance matters where you may have the same Name in more then
one Namespace or Class/Module.

For example:
Imports Microsoft.VisualBasic.Interaction
Imports MyUtilities.MyGenerics

I have an intelligent, overloaded and typesafe IIf in MyUtilities.MyGenerics
while the Interaction.IIf only works with objects. I obviously want to be
certain that MyUtilities.MyGenerics is imported first!

Hope this helps
Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2h************@uni-berlin.de...
* "Tiraman" <ti*****@netvision.net.il> scripsit:
I have an assembly that hold few class and few imports which are not used in all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....


In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #10

P: n/a
* jc********@hotmail.com (John Fiala) scripsit:
And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?


To me, it depends on how much I'm using a namespace.

Am I just using Math.Round() in one or two places? Then I'll type out
System.Math.Round - no big deal. Am I doing an intense printing job?
Then I'll Import System.Drawing.Printing. (And sometimes I do both,
on odd occasions when I want to be clear to a future developer where
something comes from.)


Yep, but even that won't have any influence on execution time.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #11

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
I normally do mine in order of importance, where order of importance does
not matter I use alphabetically.

Order of importance matters where you may have the same Name in more then
one Namespace or Class/Module.

For example:
Imports Microsoft.VisualBasic.Interaction
Imports MyUtilities.MyGenerics

I have an intelligent, overloaded and typesafe IIf in MyUtilities.MyGenerics
while the Interaction.IIf only works with objects. I obviously want to be
certain that MyUtilities.MyGenerics is imported first!


OK, that's a good idea and sometimes it's necessary.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12

P: n/a
hi every one ,

1st i would like to 10x you all for your answers .
as far as i understand you all most of you use the imports in order to save
some typing but the main issue is that it doesn't
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?

10x again .
"Tiraman" <ti*****@netvision.net.il> wrote in message
news:uN**************@TK2MSFTNGP11.phx.gbl...
Hi ,

I have an assembly that hold few class and few imports which are not used in all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?
Best Regards ,

Tiraman :-)

Nov 20 '05 #13

P: n/a
Hi Tiraman,
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?


I thought that my message did give an answer on this question.
Charles Law could have given that answer as well.

Cor
Nov 20 '05 #14

P: n/a
Tiraman,
How is Microsoft going to decide which to include on in a source? Should it
include EVERY namespace in the Framework? For a class that has 20 lines of
code you would have 100+ lines of imports. Plus as Cor stated, this may
cause problems for the IDE. When Whidbey (VS.NET 2005) is released there
will be a significant increase in the number of namespaces in the Framework.

If you are not using regular expressions, should Microsoft import the
System.Text.RegularExpression namespace? What happens when you have defined
a Match class and the System.Text.RegularExpression.Match class starts
shadowing your Match class?

If you are creating a Windows Forms application, should System.Web.UI be
imported? What happens when I actually use a class from the above namespace
in my Windows Forms app?

Microsoft does not import all the namespaces because you as developer need
to decide which namespaces are appropriate to the class/structure that you
are currently working on.

Remember that namespaces are used to organize the types (classes,
structures, delegates) in the framework & your assemblies, if you
automatically imported every thing, then you really would not need
namespaces and you would suddenly be disorganized!

Take for example the Timer class, without Namespaces which of the three
Timers are you referring to? System.Threading.Timer, System.Timers.Timer,
or System.Windows.Forms.Timer?

The namespace part of the above Timer's name is what makes each class
distinct from the other, without needing to do something funky like
"ThreadTimer", "ServerTimer", "FormTimer", which IMHO is more awkward then
using the Namespace, considering that the namespace groups related types in
a central location (System.Windows.Forms has all the Windows Forms stuff).

Hope this helps
Jay

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
hi every one ,

1st i would like to 10x you all for your answers .
as far as i understand you all most of you use the imports in order to save some typing but the main issue is that it doesn't
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?

10x again .
"Tiraman" <ti*****@netvision.net.il> wrote in message
news:uN**************@TK2MSFTNGP11.phx.gbl...
Hi ,

I have an assembly that hold few class and few imports which are not
used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?
Best Regards ,

Tiraman :-)


Nov 20 '05 #15

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Remember that namespaces are used to organize the types (classes,
structures, delegates) in the framework & your assemblies, if you
automatically imported every thing, then you really would not need
namespaces and you would suddenly be disorganized!


Namespaces help finding classes. Importing /all/ available namespaces
doesn't make sense, but importing all namespaces used in the class makes
sense. Sometimes it will be, as you say, necessary to qualify a class
to avoid ambiguity, or alternatively set up an alias for the class name
using 'Imports'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #16

P: n/a
Hi Herfried,

Try once what I said, and set that MSHTML in your import from all your
programs a week, we do not get one message anymore that week when you are
doing that.

And what I did not think about, however thought when I readed the last
message from Jay who said that explicitly, what will happen as well when you
have to much imports from large namespaces.

It makes sense as Jay said when it is possible, as what I did mean with my
message as well.

Cor

Namespaces help finding classes. Importing /all/ available namespaces
doesn't make sense, but importing all namespaces used in the class makes
sense. Sometimes it will be, as you say, necessary to qualify a class
to avoid ambiguity, or alternatively set up an alias for the class name
using 'Imports'.

Nov 20 '05 #17

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
Try once what I said, and set that MSHTML in your import from all your
programs a week, we do not get one message anymore that week when you are
doing that.

And what I did not think about, however thought when I readed the last
message from Jay who said that explicitly, what will happen as well when you
have to much imports from large namespaces.


Performance should not be a reason not to follow a certain pattern, but
in some situations there may be performance problems that force me to
not import the namespace.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #18

P: n/a
Herfried,
When I stated "imported every thing" I was referring to all 120+ namespaces
in the framework. ;-)

I agree, I normally import the 4 or 5 specific namespaces that I need in a
single class. I will occasionally import either the class (System.Math for
example) or use an Alias (Outlook for example).

Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2h************@uni-berlin.de...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Remember that namespaces are used to organize the types (classes,
structures, delegates) in the framework & your assemblies, if you
automatically imported every thing, then you really would not need
namespaces and you would suddenly be disorganized!


Namespaces help finding classes. Importing /all/ available namespaces
doesn't make sense, but importing all namespaces used in the class makes
sense. Sometimes it will be, as you say, necessary to qualify a class
to avoid ambiguity, or alternatively set up an alias for the class name
using 'Imports'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #19

P: n/a
i think that i got the idea
so 10x to all of you !

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
hi every one ,

1st i would like to 10x you all for your answers .
as far as i understand you all most of you use the imports in order to save some typing but the main issue is that it doesn't
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?

10x again .
"Tiraman" <ti*****@netvision.net.il> wrote in message
news:uN**************@TK2MSFTNGP11.phx.gbl...
Hi ,

I have an assembly that hold few class and few imports which are not
used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?
Best Regards ,

Tiraman :-)


Nov 20 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.