Solved

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

Posted on 2006-10-25
6
684 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
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
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.

 
LVL 62

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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