Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace and class conflict(?)

Tags:

c#

This is a bad title for the question, but I'm not quite sure of a better one.

I have a namespace called Globals with a class X in it. I also have a class called Globals. When I try to access Globals.X.StaticMember it tries to access the class Globals.X and complains that X doesn't exist. How do I reference the namespace Globals - ie ::Globals.X.StaticMember (:: doesn't compile).

like image 928
dan gibson Avatar asked May 07 '10 08:05

dan gibson


People also ask

What is a namespace vs class?

Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convention.

What is namespace in asp net?

Web. services namespace is a initial point for creating the web services. It contains web service classes , which allow to create XML web services using Asp.Net . XML web services provides feature to exchange messages in loosely coupled environment.


3 Answers

Try global::Globals.X.StaticMember.

And Globals are Evil!

like image 107
Snake Avatar answered Oct 30 '22 09:10

Snake


Eric Lippert recently blogged about it. Four blog posts no less, check it out. Start at part one.

like image 23
Hans Passant Avatar answered Oct 30 '22 07:10

Hans Passant


Eric Lippert recently had a series of blogposts describing the problems you run into when using the same name for a class and a namespace, explaining this statement from the Framework Design Guidelines in section 3.4:

“do not use the same name for a namespace and a type in that namespace”.

See here for the various posts:

Do not name a class the same as its namespace (Part 1, Part 2, Part 3, Part 4)

like image 45
Dirk Vollmar Avatar answered Oct 30 '22 09:10

Dirk Vollmar