Add Reference (SolutionExplorer/Using keyword)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cybertof

    Add Reference (SolutionExplorer/Using keyword)

    Hello,

    What is the difference between :
    - adding a new reference to a namespace within the SolutionExplore r
    (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 SolutionExplore r 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.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Add Reference (SolutionExplor er/Using keyword)

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19ee3a dcacb9b38198969 9@msnews.micros oft.com...[color=blue]
    > Hello,
    >
    > What is the difference between :
    > - adding a new reference to a namespace within the SolutionExplore r
    > (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 SolutionExplore r 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.[/color]


    Comment

    • Michael Bray

      #3
      Re: Add Reference (SolutionExplor er/Using keyword)

      Cybertof <cybertof2003no spam@gmx.net> wrote in
      news:MPG.19ee3a dcacb9b38198969 9@msnews.micros oft.com:
      [color=blue]
      > What is the difference between :
      > - adding a new reference to a namespace within the SolutionExplore r
      > (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 SolutionExplore r and
      > in the using statement, some other namespaces are only inside the
      > code.[/color]

      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

      Comment

      • Cybertof

        #4
        Re: Add Reference (SolutionExplor er/Using keyword)

        In article <O7pMxfajDHA.25 92@TK2MSFTNGP10 .phx.gbl>,
        nicholas.paldin o@exisconsultin g.com says...[color=blue]
        > 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:
        >[/color]


        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.

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Add Reference (SolutionExplor er/Using keyword)

          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.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - nick(dot)paldin o=at=exisconsul ting<dot>com

          "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
          news:MPG.19ee45 7f20811d2498969 a@msnews.micros oft.com...[color=blue]
          > In article <O7pMxfajDHA.25 92@TK2MSFTNGP10 .phx.gbl>,
          > nicholas.paldin o@exisconsultin g.com says...[color=green]
          > > You can not add a reference to a namespace. When you select "Add
          > > Reference", you are actually adding a reference to an assembly, which[/color][/color]
          can[color=blue][color=green]
          > > contain types in multiple namespaces.
          > > When you use the "using" statement, you are saying that for that[/color][/color]
          scope[color=blue][color=green]
          > > (typically file), you do not have to fully quantify the names of types,
          > > allowing you to say:
          > >[/color]
          >
          >
          > 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.[/color]


          Comment

          Working...