Solved

Namespace pollution  global variables

Posted on 2011-03-23
11
365 Views
Last Modified: 2012-08-14
Please explain with example.
0
Comment
Question by:KaranGupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 13

Expert Comment

by:Naman Goel
ID: 35205517
When you write using namespace X, the names in that namespace are made visible
in the current scope.

Say,
namespace X{
class Y{
};
}
class Y{
};
int main(){
Y y; // OK
}

Next write using namespace X, so class Y gets introduced in the current scope.

namespace X{
class Y{
};
}
using namespace X;
class Y
{
};
int main{
Y y; // Error...Y is ambiguous
}
0
 

Author Comment

by:KaranGupta
ID: 35212841
Hi naman_goel

The example you have given is about the class ambigousness in namespace. It is not about namespace pollution in case of global variable.


Regards
Karan Gupta
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35214237
C# doesn't have global variables, so namespace pollution is difficult to do, and very rare, but not impossible:
//namespace ConsoleApplication1
//{
	delegate void System();
	class Program
	{
		static void Main(string[] args)
		{
			// The "System" namespace is unavailable, because it conflicts
			// with the "System" delegate, which also appears in the global namespace
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
		}
	}
//}

Open in new window


I'm not sure it's possible at all in VB.Net.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KaranGupta
ID: 35214387
Hi
Thanks tgerbert for answering. Any other advice from someone.

Regards
karan
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 35215449
yes, even I agree with tgerbert, C# is component oriented language like java where we can't declare global variable outside the scope of class, and that's why we don't have such type of namespace pollution in c#.
0
 

Author Comment

by:KaranGupta
ID: 35229158
Then what is namespace pollution in vb.net
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 250 total points
ID: 35229250
that is not possible in any .net technology. The only problem you may get is what I explained earlier.
0
 

Author Comment

by:KaranGupta
ID: 35229974
Hi naman_goel

It is possible to create global variables in vb.net. You have the concept called Modules in vb.net
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35231667
But you can't declare variables outside of a module, so you can't declare them in the Global namespace.  If you declare a global variable in VB.Net it's fully qualified name becomes Global.Module1.TheVariable.
0
 

Author Comment

by:KaranGupta
ID: 35239230
Hi

Ok noted. If we say that there is a variable inside the module in vb.net can there be any namespace pollution in that case
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 35240180
No, because the variable is never in the Global namespace. In VB.Net you can never have Global.YourVariable, it would always be Global.Module1.YourVariable, or Global.Class1.YourVariable.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question