namespace not found

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am setting a string collection as:

Specialized.StringCollection s1

But I get an error saying

namespace name 'Specialized' not found.

System.Collections.Specialized.StringCollection s1 does work.

But I do have:

using System.Collections;

Why doesn't the program recognize this and force me to put the whole
namespace in?

Thanks,

Tom
 
tshad said:
I am setting a string collection as:

Specialized.StringCollection s1

But I get an error saying

namespace name 'Specialized' not found.

System.Collections.Specialized.StringCollection s1 does work.

But I do have:

using System.Collections;

Why doesn't the program recognize this and force me to put the whole
namespace in?
Because namespaces do not work that way. Or rather, using directives do not
work that way. Namespaces must always be referred to by their full name.
This arguably prevents some readability issues, because it saves you from
wondering whether "Specialized" is a namespace or a class in
System.Collections (with StringCollection as a nested class).

If you want a shorthand, then use either

using System.Collections.Specialized;

or

using StringCollection = System.Collections.Specialized.StringCollection;

The former imports the whole namespace and the latter just the class (this
is also the syntax to use for assigning aliases). In Visual Studio, you can
right-click any unresolved class name and choose "Resolve" to automatically
add the appropriate import.
 
Great thanks,

Tom

Jeroen Mostert said:
Because namespaces do not work that way. Or rather, using directives do
not work that way. Namespaces must always be referred to by their full
name. This arguably prevents some readability issues, because it saves you
from wondering whether "Specialized" is a namespace or a class in
System.Collections (with StringCollection as a nested class).

If you want a shorthand, then use either

using System.Collections.Specialized;

or

using StringCollection =
System.Collections.Specialized.StringCollection;

The former imports the whole namespace and the latter just the class (this
is also the syntax to use for assigning aliases). In Visual Studio, you
can right-click any unresolved class name and choose "Resolve" to
automatically add the appropriate import.
 
tshad said:
Great thanks,
Also as an aside,

I assume I would need both namespaces:

using System.Collections.Specialized;
using System.Collections

or in my case - 3 of them:

using System.Collections.Specialized;
using System.Collections;
using System.Collections.Generic;

using System.Collections won't do it for all of them. Only classes within
System.Collections.

Thanks,

Tom
 
Back
Top