473,394 Members | 1,932 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,394 software developers and data experts.

Using Directive/Add Reference

With

using System.Configuration

why is ConfigurationManager.AppSettings["my"]

generate an error?

Why does the error resolve itself when, in addition to the using directive,
I add a reference to System.Configuration?

Questions:
1. I understood that the using directive made the assembly available: when
does it not do so?
2. What does Add Reference do (beyond using)?

Thanks for any explanations.
Sep 18 '08 #1
3 2884
On Sep 18, 12:46*pm, AA2e72E <AA2e...@discussions.microsoft.com>
wrote:
With

using System.Configuration

why is ConfigurationManager.AppSettings["my"]

generate an error?

Why does the error resolve itself when, in addition to the using directive,
I add a reference to System.Configuration?
Because "using" directive is not the same as adding a reference.
Questions:
1. I understood that the using directive made the assembly available: when
does it not do so?
It never does that. "using" deals strictly with namespaces - it allows
you to access members of the designated namespace without fully
qualifying them. It knows nothing about assemblies at all. For
example, all your C# programs reference "mscorlib.dll", which is the
assembly where e.g. System.Console lives, regardless of whether you
have "using System" in your code, or not.
2. What does Add Reference do (beyond using)?
It references the specified assembly, allowing you to use the types
within it - nothing less, nothing more.
Sep 18 '08 #2
A reference is required to say "I want to use this dll". In this case,
the code you want is in System.Configuration.dll, so you need to add a
reference to System.Configuration.dll.

A "using" statement is simply a finger-saving tool inside a single file
/ etc. It says "when resolving types, look in this namespace too". It
doesn't cause the code to import any external dlls.

Without the "using" statement, you would have to give the
fully-qualified name of the type:

System.Configuration.ConfigurationManager.AppSetti ngs["my"]

with the "using System.Configuration;" statement, when looking for
ConfigurationManager it knows to check in the System.Configuration
namespace, so you can just type:

ConfigurationManager.AppSettings["my"].

But without the reference to the dll, neither of these will work.

Marc
Sep 18 '08 #3
To extend on previous answers, using is actually for giving namespaces an
alias for use in that code file so if you have a namespace called

my.really.long.namspace.which.I.Dont.want.to.type

you could put at the top

using LongOne = my.really.long.namspace.which.I.Dont.want.to.type;

then use classes in that namespace as LongOne.ClassA

When you dont put the alias = bit, the compile takes that to mean you want
the classes to be accessible without specifying the namespaces. Which is fine
unless there are classes in available class names, which you will
individually need to qualify when you use them.

Hope that helps

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"AA2e72E" wrote:
With

using System.Configuration

why is ConfigurationManager.AppSettings["my"]

generate an error?

Why does the error resolve itself when, in addition to the using directive,
I add a reference to System.Configuration?

Questions:
1. I understood that the using directive made the assembly available: when
does it not do so?
2. What does Add Reference do (beyond using)?

Thanks for any explanations.
Sep 18 '08 #4

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

Similar topics

2
by: Reena | last post by:
Hi, Working on .NET 2003. Using C# as server code. I have created a class with following code... using System; using System.Data.OracleClient; using System.Web; using...
1
by: SD WinGirl | last post by:
Is there anyway to programmatically determine which reference assembly to add when the code has a using directive? For example if the code has using System.XML; I need to add a reference to the...
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...
2
by: Tim Wallace | last post by:
I'm a .Net noob. I created a dll using c#. Now, I want to reference that DLL in an ASPX page. I put the import directive in the page like this: <%@ Import Namespace="BHQ.BusinessLogicLayer" %>...
5
by: Sharon Tal | last post by:
Compiler Error Message: CS0234: The type or namespace name 'Http' does not exist in the class or namespace 'System.Runtime.Remoting.Channels' (are you missing an assembly reference?) Source...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
2
by: Andy Fish | last post by:
Hi, I have a single-file aspx file (no codebehind). when I try to do this: <%@ Import Namespace="MySql.Data.MySqlClient" %> I get this: CS0246: The type or namespace name 'MySql' could...
3
by: VRSki | last post by:
Hello, Is there any way to use "using" for aliasing in the global scope? The example below works fine in the context of a given file, but in the different file MyByte alias is unknown. Is...
2
by: m.a | last post by:
Hello, I have the following code which is an entity for a table in my database. but I am getting several errors such as: The type or namespace name 'ColumnAttribute' could not be found (are...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.