Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using statements inside namespace

9 views
Skip to first unread message

Anders Eriksson

unread,
Apr 23, 2013, 4:50:21 AM4/23/13
to
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.

bradbury9

unread,
Apr 23, 2013, 5:06:33 AM4/23/13
to
You can use a namespace alias, check this link:
http://msdn.microsoft.com/en-us/library/c3ay4x3d%28v=vs.80%29.aspx

bradbury9

unread,
Apr 23, 2013, 5:12:26 AM4/23/13
to
El martes, 23 de abril de 2013 10:50:21 UTC+2, Anders Eriksson escribió:
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%28v=vs.80%29.aspx

Anders Eriksson

unread,
Apr 23, 2013, 5:46:10 AM4/23/13
to
On 2013-04-23 11:12, bradbury9 wrote:
>
> 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
Yes, 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!

Arne Vajhøj

unread,
Apr 23, 2013, 5:45:11 PM4/23/13
to
On 4/23/2013 4:50 AM, Anders Eriksson wrote:
> 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


Arne Vajhøj

unread,
Apr 23, 2013, 5:50:28 PM4/23/13
to
On 4/23/2013 5:06 AM, bradbury9 wrote:
> 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.

>> =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'

> You can use a namespace alias, check this link:
> http://msdn.microsoft.com/en-us/library/c3ay4x3d%28v=vs.80%29.aspx

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%28v=vs.110%29.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





0 new messages