Solved

Namespace pollution  global variables

Posted on 2011-03-23
11
354 Views
Last Modified: 2012-08-14
Please explain with example.
0
Comment
Question by:KaranGupta
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now