Solved

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

Posted on 2006-10-25
6
676 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

19 Experts available now in Live!

Get 1:1 Help Now