473,473 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using - am I missing something

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 'using' statements put
there automatically by the code generator - for example 'using System' ,and
'using System.Data.
Below that is my class, also with some automaticcaly generated code,
starting with:
protected System.Data.OleDb.OleDbConnection oleDbConnection1;
protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter2;
etc.
If I modify these to
protected OleDb.OleDbConnection oleDbConnection1;
protected OleDb.OleDbDataAdapter oleDbDataAdapter2;
then the build fails.
Can someone please explain why these need to have System.Data specified,
when this namespace is already specified in a 'using' statement.
--
Dave
Nov 17 '05 #1
7 1202
Can someone please explain why these need to have System.Data specified,
when this namespace is already specified in a 'using' statement.


Because System.Data != System.Data.OleDb, even though they happen to
share a common prefix. If you add

using System.Data.OleDb;

you can then change the code to

protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
In addition to what Mattias said,

if using System.Data should make your statement work then question is just
by writing System every thing should work,

e.g.
using System;
....
....
OleDbConnection oleDbConnection1

it should create a object too, right? After all it is some place inside
System namespace.

Actually, it do not work like that. Reason is that using makes methods,
interface, and other stuff inside that namespace visible but not a namespace
in side it for inline use. One of the reason I can think of is that I can
have my own namespace named to be OleDB and it could have all the stuff
other than what you are hoping to see or something that you do not want but
is used with totally different output. Very much like having the same method
name in visible namespace structures (which actually gives compile error,
and intellisense shows it as read dot in front). In that condition you will
have to fully qualify it to avoid any error. If just using higher namespace
structure solves the problem then there will be lots of name collision which
will defeat the real purpose behind namespacing.

That is the base idea behind namespacing anyways, to avoid collisions while
naming.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Can someone please explain why these need to have System.Data specified,
when this namespace is already specified in a 'using' statement.


Because System.Data != System.Data.OleDb, even though they happen to
share a common prefix. If you add

using System.Data.OleDb;

you can then change the code to

protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #3
You didn't read my post properly - I didn't change the code to
protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;
I changed it to
protected OleDb.OleDbConnection oleDbConnection1;
protected OleDb.OleDbDataAdapter oleDbDataAdapter2;
--
Dave
"Mattias Sjögren" wrote:
Can someone please explain why these need to have System.Data specified,
when this namespace is already specified in a 'using' statement.


Because System.Data != System.Data.OleDb, even though they happen to
share a common prefix. If you add

using System.Data.OleDb;

you can then change the code to

protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #4
I see.
So I can put
System.Data.OleDb.OleDbConnection oleDbConnection1;
or I can put
using System.Data.OleDb
OleDbConnection oleDbConnection1;
but I can't put
using System.Data
OleDb.OleDbConnection oleDbConnection1;

I suppose it makes a sort of sense - it avoids a clash if two namespaces
both contain namespaces with the same name. But then it's only the same clash
that would happen if two namespaces contain objects with the same name. At
the end of the day, 'using' is a convenient shorthand, but if clashes occur
you have to fully specify, so why not allow a partial specification - if
there's no clash you're OK, and if there is a clash you're no worse off, you
just have to specify more precisely.

It must be the same in C++ but oddly I never hit it before.
--
Dave
"Pohihihi" wrote:
In addition to what Mattias said,

if using System.Data should make your statement work then question is just
by writing System every thing should work,

e.g.
using System;
....
....
OleDbConnection oleDbConnection1

it should create a object too, right? After all it is some place inside
System namespace.

Actually, it do not work like that. Reason is that using makes methods,
interface, and other stuff inside that namespace visible but not a namespace
in side it for inline use. One of the reason I can think of is that I can
have my own namespace named to be OleDB and it could have all the stuff
other than what you are hoping to see or something that you do not want but
is used with totally different output. Very much like having the same method
name in visible namespace structures (which actually gives compile error,
and intellisense shows it as read dot in front). In that condition you will
have to fully qualify it to avoid any error. If just using higher namespace
structure solves the problem then there will be lots of name collision which
will defeat the real purpose behind namespacing.

That is the base idea behind namespacing anyways, to avoid collisions while
naming.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Can someone please explain why these need to have System.Data specified,
when this namespace is already specified in a 'using' statement.


Because System.Data != System.Data.OleDb, even though they happen to
share a common prefix. If you add

using System.Data.OleDb;

you can then change the code to

protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 17 '05 #5
partial specification !!
well think this way, what if by hard luck all my namespaces, methods,
interface etcs under a xyz namespace are matching with your namespace abc.
Will you be willing to specify each function etc ? I will not. Basically it
makes life easy, in fact very easy.

--
Po

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
I see.
So I can put
System.Data.OleDb.OleDbConnection oleDbConnection1;
or I can put
using System.Data.OleDb
OleDbConnection oleDbConnection1;
but I can't put
using System.Data
OleDb.OleDbConnection oleDbConnection1;

I suppose it makes a sort of sense - it avoids a clash if two namespaces
both contain namespaces with the same name. But then it's only the same
clash
that would happen if two namespaces contain objects with the same name. At
the end of the day, 'using' is a convenient shorthand, but if clashes
occur
you have to fully specify, so why not allow a partial specification - if
there's no clash you're OK, and if there is a clash you're no worse off,
you
just have to specify more precisely.

It must be the same in C++ but oddly I never hit it before.
--
Dave
"Pohihihi" wrote:
In addition to what Mattias said,

if using System.Data should make your statement work then question is
just
by writing System every thing should work,

e.g.
using System;
....
....
OleDbConnection oleDbConnection1

it should create a object too, right? After all it is some place inside
System namespace.

Actually, it do not work like that. Reason is that using makes methods,
interface, and other stuff inside that namespace visible but not a
namespace
in side it for inline use. One of the reason I can think of is that I can
have my own namespace named to be OleDB and it could have all the stuff
other than what you are hoping to see or something that you do not want
but
is used with totally different output. Very much like having the same
method
name in visible namespace structures (which actually gives compile error,
and intellisense shows it as read dot in front). In that condition you
will
have to fully qualify it to avoid any error. If just using higher
namespace
structure solves the problem then there will be lots of name collision
which
will defeat the real purpose behind namespacing.

That is the base idea behind namespacing anyways, to avoid collisions
while
naming.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
>
>>Can someone please explain why these need to have System.Data
>>specified,
>>when this namespace is already specified in a 'using' statement.
>
> Because System.Data != System.Data.OleDb, even though they happen to
> share a common prefix. If you add
>
> using System.Data.OleDb;
>
> you can then change the code to
>
> protected OleDbConnection oleDbConnection1;
> protected OleDbDataAdapter oleDbDataAdapter2;
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.


Nov 17 '05 #6
In message <B7**********************************@microsoft.co m>, Dave
<Da**@discussions.microsoft.com> writes
I see.
So I can put
System.Data.OleDb.OleDbConnection oleDbConnection1;
or I can put
using System.Data.OleDb
OleDbConnection oleDbConnection1;
but I can't put
using System.Data
OleDb.OleDbConnection oleDbConnection1;

I suppose it makes a sort of sense - it avoids a clash if two namespaces
both contain namespaces with the same name. But then it's only the same clash
that would happen if two namespaces contain objects with the same name.


One of those things, I think, where any increase in convenience would be
outweighed by the increase in complexity. The way it currently works
makes it very explicit and concise.

You can access member namespaces of an aliased namespace, though:

using Data = System.Data;
Data.SqlClient.SqlCommand cmd = new Data.SqlClient.SqlCommand();

I don't think I like that very much, I have to say.

--
Steve Walker
Nov 17 '05 #7
You didn't read my post properly
Yes I did.

- I didn't change the code to
protected OleDbConnection oleDbConnection1;
protected OleDbDataAdapter oleDbDataAdapter2;


I know you didn't - I did, to show you a working alternative solution.

The "partial specification" feature you're asking for is how Imports
works in VB.NET, but not how using works in C#.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #8

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

Similar topics

2
by: Cantor | last post by:
Hello everyone :) I am writing some dotNET code to "consume" a web service written in PHP. So far I have been getting some error messages and PHP seems to place HTML text in front of the XML...
3
by: John Reese | last post by:
Hello there. I've run into some missing functionality with HTTP Digest authentication in the 2.3 library and I was wondering if I'm just missing something. Missing functionality the first:...
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
9
by: Nathan Sokalski | last post by:
I am trying to do a database search using LIKE using the following code: Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click If...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
5
by: kewalmehra | last post by:
Hi All, I have a requirement of saving Excel files as HTML using C#. I have managed to write code for saving as HTLM file. however this seems to be not working in the once perticular case . ...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: liam_jones | last post by:
I'm very new to Python, well IronPython to precise, and have been having problems when using Excel. The problem I'm having is the closing of my Excel object. I'm able to successfully quit the...
5
Atli
by: Atli | last post by:
Hi guys. So, I got it into my head this morning that I needed a Linux HTTP server. Naturally that means installing Apache with PHP and last but not least, MySQL. I decided to use Fedora Code...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.