Add Reference (SolutionExplorer/Using keyword)

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

Hello,

What is the difference between :
- adding a new reference to a namespace within the SolutionExplorer
(right click, Add Reference...)
- adding a new reference with the 'using' keyword in the source code

For example, System.Windows.Forms is both in the SolutionExplorer and in
the using statement, some other namespaces are only inside the code.

Are these 2 differents things ?
Is there anything to do with intelissense ?

Regards,
Cybertof.
 
Cybertof,

You can not add a reference to a namespace. When you select "Add
Reference", you are actually adding a reference to an assembly, which can
contain types in multiple namespaces.

When you use the "using" statement, you are saying that for that scope
(typically file), you do not have to fully quantify the names of types,
allowing you to say:

// Create a new button.
Button pobjButton = new Button();

Instead of having to do:

// Create a new button.
System.Windows.Forms.Button pobjButton = new System.Windows.Forms.Button();

It just so happens that some assembly names coincide with the namespace
names that some of the contained types are members of.

Hope this helps.
 
What is the difference between :
- adding a new reference to a namespace within the SolutionExplorer
(right click, Add Reference...)
- adding a new reference with the 'using' keyword in the source code

For example, System.Windows.Forms is both in the SolutionExplorer and
in the using statement, some other namespaces are only inside the
code.

They are indeed two different things.

First, when you 'Add Reference...' you aren't really doing anything with
namespaces, per se. You are adding a reference to a DLL which contains
one or more namespaces, each containing one or more objects (classes,
enums, structs, etc...)

In order to make things easier on you and the IDE, the IDE/compiler
wants to know what namespaces it should search in when you type
something like

Form x = new Form()

Form is actually in the System.Windows.Forms namespace, inside the
System.Windows.Forms.DLL file (the fact that they are named the same is
for convenience - there are other namespaces available in this same
DLL). If you didn't have the line "using System.Windows.Forms" at the
top of your file, you would have to explicitly say

System.Windows.Forms.Form x = new System.Windows.Forms.Form()

If you are curious and want to see what namespaces are in any particular
'Add Reference...' item, open the 'References' group in the Solution
Explorer and double click one of them (or right-click and select 'View
in Object Browser'). Anything with a pair of braces ("{}") is a
namespace that you can reference with a "using..." line.

-mbray
 
You can not add a reference to a namespace. When you select "Add
Reference", you are actually adding a reference to an assembly, which can
contain types in multiple namespaces.
When you use the "using" statement, you are saying that for that scope
(typically file), you do not have to fully quantify the names of types,
allowing you to say:


So if I understand, I cannot use a namespace with the 'using' keyword if
the assembly containing this namespace has not been added to the
Solution with 'Add Reference' ?

Am I right ?

Regards,
Cybertof.
 
Cybertof,

Absolutely. You need to set the assembly reference first, then you can
declare the shorthand (using "using") in your files to access the types.
 
Back
Top