Namespace snags?

  • Thread starter Thread starter BH
  • Start date Start date
B

BH

Hi C# & .NET detectives,

can you help with two problems I've encountered lately?

1) These seem equivalent to me but one fails and the other doesn't.

This fails with "type or namespace 'Security' could not be found" message:
//-----------
using System;
using System.Threading;
//...later...
Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//--------------

However, this succeeds:
//----------
// using System ...code now uses explicit System reference
using System.Threading;
//...later...
System.Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//----------


2) OK, how about this problem
This fails with "type or namespace STAThread cannot be found" message...

[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

How do I use/refer to STAThread correctly?

Thanks for your time,

BH
 
Hi BH,
The answer of your first question can be found in MSDN. The following is
quote from MSDN:
"... Create a using directive to use the types in a namespace without having
to specify the namespace. A using directive does not give you access to any
namespaces that may be nested in the namespace you specify. ..."

*Security* is a namespace.

About the second question. STAThreadAttribute class is defined in System
namespace.
You can either use fully qualified name:

[System.STAThread]
static void Main()
{
Application.Run(new MainForm());
}

or use *using* directive:

using System;

[STAThread]
static void Main()
{
Application.Run(new MainForm());
}


HTH
B\rgds
100
 
BH,

The reason the first doesn't work is that using declarations do not act
as shortcuts for other namespaces, only for types that are within the
namespaces that you use in the "using" statements. So, you have to put
System.Security, etc, etc.

The reason that the STAThread attribute is not found is because you took
out the "using System;" directive, so it doesn't know which namespace
STAThread is in. If you place "using System;" back at the top, it will find
it.

Hope this helps.
 
Thanks folks... problems solved, lessons learned.

BH

Nicholas Paldino said:
BH,

The reason the first doesn't work is that using declarations do not act
as shortcuts for other namespaces, only for types that are within the
namespaces that you use in the "using" statements. So, you have to put
System.Security, etc, etc.

The reason that the STAThread attribute is not found is because you took
out the "using System;" directive, so it doesn't know which namespace
STAThread is in. If you place "using System;" back at the top, it will find
it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

BH said:
Hi C# & .NET detectives,

can you help with two problems I've encountered lately?

1) These seem equivalent to me but one fails and the other doesn't.

This fails with "type or namespace 'Security' could not be found" message:
//-----------
using System;
using System.Threading;
//...later...
Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//--------------

However, this succeeds:
//----------
// using System ...code now uses explicit System reference
using System.Threading;
//...later...
System.Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//----------


2) OK, how about this problem
This fails with "type or namespace STAThread cannot be found" message...

[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

How do I use/refer to STAThread correctly?

Thanks for your time,

BH
 
If you would like to use Security to identify any of the types within the
System.Security namespace, you can implement your using line as: "using
Security = System.Security;". This also helps if you have to reference two
namespaces which have members with the same name, thus causing the compiler
to not be able to determine which type you mean.

Steve


BH said:
Thanks folks... problems solved, lessons learned.

BH

in message news:%[email protected]...
BH,

The reason the first doesn't work is that using declarations do not act
as shortcuts for other namespaces, only for types that are within the
namespaces that you use in the "using" statements. So, you have to put
System.Security, etc, etc.

The reason that the STAThread attribute is not found is because you took
out the "using System;" directive, so it doesn't know which namespace
STAThread is in. If you place "using System;" back at the top, it will find
it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

BH said:
Hi C# & .NET detectives,

can you help with two problems I've encountered lately?

1) These seem equivalent to me but one fails and the other doesn't.

This fails with "type or namespace 'Security' could not be found" message:
//-----------
using System;
using System.Threading;
//...later...
Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//--------------

However, this succeeds:
//----------
// using System ...code now uses explicit System reference
using System.Threading;
//...later...
System.Security.Principal.IPrincipal user = Thread.CurrentPrincipal;
//----------


2) OK, how about this problem
This fails with "type or namespace STAThread cannot be found" message...

[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

How do I use/refer to STAThread correctly?

Thanks for your time,

BH
 
Back
Top