Solved

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

Posted on 2006-10-25
6
682 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now