Using statements inside namespace

A

Anders Eriksson

I have an ActiveX library that I need to use to control some hardware.
This ActiveX has a public interface called System.
I know that it's stupid but it's a fact!

If I put the using statements before my namespace everything works, but
if I put them inside my namespace (like Stylecop wants) The
sub-type names of System will not be found since System is assumed to be
in the ActiveX namespace.

Why and can I do anything about it and still have the using within my
namespace?

=This works=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using laserengineLib; // this is the Interop for the ActiveX library

namespace DcSelectMark
{

=This doesn't work=
namespace DcSelectMark
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using laserengineLib; // this is the Interop for the ActiveX library

I get this error message:
namespace System.Collections
Error:
The type name 'Collections' does not exist in the type
'laserengineLib.System'

I get the same error for
System.ComponentModel
System.Data
System.Drawing
System.IO
System.Text
System.Windows

// Anders
 
B

bradbury9

El martes, 23 de abril de 2013 10:50:21 UTC+2, Anders Eriksson escribió:
I have an ActiveX library that I need to use to control some hardware.

This ActiveX has a public interface called System.

I know that it's stupid but it's a fact!



If I put the using statements before my namespace everything works, but

if I put them inside my namespace (like Stylecop wants) The

sub-type names of System will not be found since System is assumed to be

in the ActiveX namespace.



Why and can I do anything about it and still have the using within my

namespace?



=This works=

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Text;

using System.Windows.Forms;

using laserengineLib; // this is the Interop for the ActiveX library



namespace DcSelectMark

{



=This doesn't work=

namespace DcSelectMark

{

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Text;

using System.Windows.Forms;

using laserengineLib; // this is the Interop for the ActiveX library



I get this error message:

namespace System.Collections

Error:

The type name 'Collections' does not exist in the type

'laserengineLib.System'



I get the same error for

System.ComponentModel

System.Data

System.Drawing

System.IO

System.Text

System.Windows



// Anders



--

English isn't my first language.

So any error or strangeness is due to the translation.

Please correct my English so that I may become better.

You can use a namespace alias, check this link:
http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspx
 
B

bradbury9

El martes, 23 de abril de 2013 10:50:21 UTC+2, Anders Eriksson escribió:
I have an ActiveX library that I need to use to control some hardware.

This ActiveX has a public interface called System.

I know that it's stupid but it's a fact!



If I put the using statements before my namespace everything works, but

if I put them inside my namespace (like Stylecop wants) The

sub-type names of System will not be found since System is assumed to be

in the ActiveX namespace.



Why and can I do anything about it and still have the using within my

namespace?



=This works=

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Text;

using System.Windows.Forms;

using laserengineLib; // this is the Interop for the ActiveX library



namespace DcSelectMark

{



=This doesn't work=

namespace DcSelectMark

{

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Text;

using System.Windows.Forms;

using laserengineLib; // this is the Interop for the ActiveX library



I get this error message:

namespace System.Collections

Error:

The type name 'Collections' does not exist in the type

'laserengineLib.System'



I get the same error for

System.ComponentModel

System.Data

System.Drawing

System.IO

System.Text

System.Windows



// Anders



--

English isn't my first language.

So any error or strangeness is due to the translation.

Please correct my English so that I may become better.

BTW, I thought that the 'using' should be always before the namespace declaration, althought I have not found the reference to such rule. Here is somemore info on namespaces and aliases. http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspx
 
A

Anders Eriksson

BTW, I thought that the 'using' should be always before the namespace declaration,althought I have not found the reference to such rule. Here is some more
info on namespaces and aliases.
http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.80).aspxYes, I have also always thought so, but according to Stylecop Rule
SA1200 it doesn't seem to be so.

Anyway I have fixed this the easy way, by unchecking this rule and
keeping the using where they always has been.

Thank you for you input!

// Anders
 
A

Arne Vajhøj

I have an ActiveX library that I need to use to control some hardware.
This ActiveX has a public interface called System.
I know that it's stupid but it's a fact!

If I put the using statements before my namespace everything works, but
if I put them inside my namespace (like Stylecop wants) The
sub-type names of System will not be found since System is assumed to be
in the ActiveX namespace.

Why and can I do anything about it and still have the using within my
namespace?

=This works=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using laserengineLib; // this is the Interop for the ActiveX library

namespace DcSelectMark
{

That is how I write C# code as well.

And how I see MS and many other write code.

(except that I would put an empty line between System and non-System
using's)

Arne
 
A

Arne Vajhøj

El martes, 23 de abril de 2013 10:50:21 UTC+2, Anders Eriksson escribió:

I agree that:

using global::System;

is a good solution (given an requirement to put using's at
that place).

But I get different associations by the term "namespace alias".


Note that the latest version of the doc:

http://msdn.microsoft.com/en-us/library/c3ay4x3d(v=vs.110).aspx

has changed the term to "Global Namespace Alias" which I think is better.

"namespace alias" is usually associated with:

http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx

section "Namespace Aliases".

Arne
 

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