Solved

Namespace pollution  global variables

Posted on 2011-03-23
11
364 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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