473,378 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

using namespaces

I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??
Nov 18 '05 #1
5 1587
The goal of importing a namespace into a file is to increase readability,
having it recurse down an entire namespace hierachy defeats that purpose...

Imagine the following

using System;

namespace test{
public class utility{
Button b = new Button();
}
}

How would you ever know (or the compiler) whether that was
System.Windows.Forms.Button or System.Web.UI.WebControls.Button ??

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Davíð Þórisson" <db**@hi.is> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??

Nov 18 '05 #2
thank you Karl
my question though is more concerning why I have to import both the top
level and also sub level namespaces eg I would have thought
using System;

would import all sub-namespaces eg. System.Data;

BTW, if the object - lets say the button you refer to - is not specified by
importing the according namespace, would that return an error or would ASP
try to guess which one I'm refering to??
"Karl"
The goal of importing a namespace into a file is to increase readability,
having it recurse down an entire namespace hierachy defeats that
purpose...

Imagine the following

using System;

namespace test{
public class utility{
Button b = new Button();
}
}

How would you ever know (or the compiler) whether that was
System.Windows.Forms.Button or System.Web.UI.WebControls.Button ??

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Davíð Þórisson" <db**@hi.is> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??


Nov 18 '05 #3
I think my original reply answered your question. If by importing System it
also imported System.Data, System.Web, System.Web.UI,
System.Web.UI.WebControls, System.Windows, System.Windows.Forms and
everything else you'd defeat the purpose of the using statement. There
are a number of comflicting class names throughout the System namespace (and
others) and only specifying a specific namespace can resolve this ambiguity.

If the object is not specified by importnig the appropriate namespace, the
code won't compile. you cannot have any ambiguity when refering to
objects...the namespace must be clear and well defined. In the example I
gave bellow, I won't even be able to build the code.

Something else ot keep in mind is that you don't need to use using, for
example, the following is valid:

using System;
using System.Web.UI.Webcontrols;

public System.Windows.Form.Button b1= new System.Windows.Form.Button();
public Button b2 = new Button(); //refers to
System.Web.UI.WebControls.Button

or you could use aliases:

using form = System.Windows.Form;
using web = System.Web.UI.WebControls;

public form.Button b1 = new form.Button();
public web.Button b2 = new web.Button();
But again, all this only works because importing a namespace doesn't
automatically import everything beneath it...it would be chaos (absolute and
total chaos!!) if it did that ;)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Davíð Þórisson" <db**@hi.is> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
thank you Karl
my question though is more concerning why I have to import both the top
level and also sub level namespaces eg I would have thought
using System;

would import all sub-namespaces eg. System.Data;

BTW, if the object - lets say the button you refer to - is not specified by importing the according namespace, would that return an error or would ASP
try to guess which one I'm refering to??
"Karl"
The goal of importing a namespace into a file is to increase readability, having it recurse down an entire namespace hierachy defeats that
purpose...

Imagine the following

using System;

namespace test{
public class utility{
Button b = new Button();
}
}

How would you ever know (or the compiler) whether that was
System.Windows.Forms.Button or System.Web.UI.WebControls.Button ??

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Davíð Þórisson" <db**@hi.is> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??



Nov 18 '05 #4
You are assuming there is a namespace hierarchy with system being on the top
level and system.data one level down, something similar to a file system
where directory \temp\work is located under directory \temp. This assumption
is not correct. system is located in file system.dll and system.data in
system.data.dll. They are not parts of the same hierarchy, rather two
separate libraries. Therefore, when you declare system, it has nothing to do
with system.data.

Eliyahu

"Davíð Þórisson" <db**@hi.is> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??

Nov 18 '05 #5
Davíð Þórisson wrote:
I've never understood fully, why do I have to td eg:
using system;
using system.data;

I would have thought that the top line imports system and all of its
submembers, including data??


You don't specifically *need* to use the "using" statement, you can also
use the full classname. "using" is just there so you don't have to specify
the namespace of the classes you want to use.

Don't confuse "using" with "project references" which you *do* need.
Here you don't point to a specific namespace but to an assembly instead.
There might be some relation between assembly name and (some) contained
namespace, but that's "coincidence". An assembly can contain any number
of namespaces.
When you have a reference to an assembly, you can use any (public) class
in it, by specifying the full classname.

Hans Kesting
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
4
by: Cybertof | last post by:
Hello, What is the difference between : - adding a new reference to a namespace within the SolutionExplorer (right click, Add Reference...) - adding a new reference with the 'using' keyword in...
7
by: Dave | last post by:
I'm fairly new to C# and I must be misunderstanding something: I have created a standard web ASP.NET web application project and put data controls into it. Right at the top is a whole set of...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
3
by: Yair Cohen | last post by:
hello 1 i use an xml documet with xml schema but when i try to use XPath (selectnodes) it retrieves empty list. when i remove the schema (the 'xmlns="http..."') i get the data. how can i handle...
3
by: Asfand Yar Qazi | last post by:
For years, I've been putting everything that won't be needed outside a compilation unit in anonymous namespaces, even editing my old files that did things the 'old' way (using static linkage). ...
21
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input');...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
6
by: Juha Nieminen | last post by:
Whenever one sees example C++ code basically anywhere, be it in a book, in a tutorial in the internet, in an online forum or whatever, I would estimate that at least in 99% of cases one sees the...
4
by: coal | last post by:
Does anyone ponder what life would be like if we stopped using namespaces? Instead of namespaces, a website could be used to propose/ register names. Namespaces were designed when the internet...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.