Why put references in two places?

B

Bruce W.1

What's the point of putting a reference (eg. to System.Data) in the
Solution Explorer AND at the top of the code files (using in C# and
Imports in VB)?

Is there some purpose to this redundancy?

What breaks if one of them is omitted?

Thanks for your help.
 
S

Shan Plourde

Actually specifying Imports / using in your source code files is not
necessary. The sole purpose of it is so that you don't have to specify
the full namespace to classes all throughout your source code.

Meaning if I have a c# class such as:

using MyNameSpace;

public MyMethod()
{
MyType obj = new MyType();
}

I can not specify the namespace, but then I have to fully qualify each
and every class. So if I remove the using above, then MyMethod must
change to:

public MyMethod()
{
MyNameSpace.MyType obj = new MyNameSpace.MyType();
}

That's the only benefit that you get by using the "using" declaration.
On the other hand you _must_ add the references to the assemblies/dlls
in the Solution Explorer. Those references to dlls are required by the
compiler, and of course at runtime. They also allow the nice things that
the IDE provides such as code completion.

So there is no redundancy after all :p

Shan
 
P

Phill. W

Bruce W.1 said:
What's the point of putting a reference (eg. to System.Data) in the
Solution Explorer AND at the top of the code files (using in C# and
Imports in VB)?

You're comparing Apples and Oranges here, Bruce.

Adding a reference makes the stuff in the referenced Dll available
to your program. Omit the reference and all Hell breaks loose.

Adding an Imports or Using statement just reduces the amount of
code you have to type (but not what it gets compiled into).

Try this snippet with and without the Imports statement.

Imports System.Drawing.SystemIcons

Dim Icon1 As System.Drawing.Icon _
= Asterisk
Dim Icon2 As System.Drawing.Icon _
= System.Drawing.SystemIcons.Asterisk

Oh; you'll need a Reference to System.Drawing.dll before you start ;-)

HTH,
Phill W.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top