Solved

Difference between Imports System.Configuration vs. Adding Reference with System.configuration.dll

Posted on 2006-10-25
6
688 Views
Last Modified: 2010-04-24
Good Day,

I'm working with VB.NET 2005 and I was attempting to access the ConfigurationManager Class.  When I looked at the documentation, it says that the namespace needed is System.Configuration.

Beautiful.

But my code wouldn't compile and I couldn't figure out what I was doing wrong.

Someone suggested that I add the .DLL as a reference to my project and everything now works.

So my question is, What is the difference between the two?  I thought if I included the Imports System.Configuration namespace, that I would have access to allthe classes under the System.Configuration namespace.  If that's the case, why would I need the DLL?

I've only offered 50 points, so if the answer is detailed, I'll raise the point level.

coson
0
Comment
Question by:coson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17806530
from what i understand, the namespaces reside on the dll's, or projects. If you import the System.Xml namespace, you will find that it also adds a reference to System.Xml dll, since they are not mutually exclusive but, au contraire, they are the same thing.
Namespaces on the imports statement do not add the reference per se, what they do is just to allow you to use the classes inside the dll without all the name like System.Xml.DoMethod1(), and just use DoMethod1() instead.
In cases where 2 methods are named the same in various dll's, you will still need to reference using the full name to avoid confusions.

hope it helps
NY
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 50 total points
ID: 17806543
in summary, if you want to use classes that reside in a dll, you need to add a reference to it. if you want to call them up in your code without using the full name to call it, import the namespace.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17806547
in that order.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17806620
From the Microsoft documentation

Assemblies include one or more namespaces. When you add a reference to an assembly, you can also add an Imports statement to a module that controls the visibility of that assembly's namespaces within the module. The Imports statement provides a scoping context that lets you use only the portion of the namespace necessary to supply a unique reference.

http://msdn2.microsoft.com/en-us/library/h9st4tss.aspx

Fernando
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17806624
Ok..
the difference is that the imports statement is used to shorten the length of code you type (i.e. the namespace name) let's say you are using regularexpressions... the classes are located at System.Text.RegularExpressions

so if you declare a variable of type regex
Dim aa As System.Text.RegularExpressions.Regex
and the options would be like this System.Text.RegularExpressions.RegexOptions

if you add imports System.Text.RegularExpressions
then it is shortened to
Dim aa As regex
and the options would be like this RegexOptions

but you must add the reference to the dll to be able to import the namespaces and access its internal types.
so imports is only to ease the work but the references are necessary to use the dlls...

you could see the default imports... if you click on the project menu then project propertied you'll find the default imports in the tree item imports of Common properties in the tree on the left
0
 

Expert Comment

by:pthomassr
ID: 20295672
I still don't get it.  I added the "Imports System.Configuration" statement at the top of the module, and coded "strDBLocation = ConfigurationManager.AppSettings("DBLocation")" and got "ConfigurationManager is not declared" as an error.  So... what are the EXACT statements for the Imports statements, and the EXACT code to reference the proper class and method to extract a value from the web.config file?  Apparently, there is no "ConfigurationManager" in System.Configuration, or I have no reference to it yet.  Do you have a code sample that shows how to get a dbconnection string in VB.Net 2005 that will syntax properly?  If there are other steps outside code, can I also get detail steps to perform them?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

630 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