Solved

Namespace pollution  global variables

Posted on 2011-03-23
11
362 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

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

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 20
Disable extension 8 36
VB: Convert 2 dates to specific format 24 48
Please explain the parts of these 2 LINQ expressions 3 26
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

11 Experts available now in Live!

Get 1:1 Help Now