0

I am trying to use the values specified by an App.config file in a C# program in Visual Studio 2010, based on the directions specified in the "accepted answer" to this post: Custom app.config Config Section Handler

My code file is using System.Configuration, and I'm trying to create the following:

public class ConnectionCollection : ConfigurationElementCollection {
    //code here
}

public class ConnectionElement : ObjectConfigurationElement {
    //code here
}

However, the error messages say that ConfigurationElementCollection, ObjectConfigurationElement and ConfigurationElement can't be found. What am I doing wrong?

Community
  • 1
  • 1
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • Which version of the framework are you using? – Oscar Feb 28 '14 at 20:43
  • c#.net in visual studio 2010 – La-comadreja Feb 28 '14 at 20:47
  • That's the development environment version, not the framework version used in your project. By default it's 4.0 for VS2010. Also, have you added a reference to System.Configuration assembly? – Oscar Feb 28 '14 at 20:50
  • Yes, I added the reference to System.Configuration and it still doesn't work. Target framework is .NET framework 4. – La-comadreja Feb 28 '14 at 20:54
  • What is this `ObjectConfigurationElement`? The `ConfigurationElementCollection` should be fine after referencing the assembly, but I don't see any `ObjectConfigurationElement`. Only `ConfigurationElement`: http://msdn.microsoft.com/en-us/library/system.configuration.configurationelement%28v=vs.110%29.aspx – Csaba Toth Feb 28 '14 at 21:07
  • I wonder if it's a version issue then. – La-comadreja Feb 28 '14 at 21:10
  • `ObjectConfigurationElement` doesn't exist in the .Net framework. It's a custom class from here - http://sourceforge.net/p/nmailserver/code/HEAD/tree/NMail/trunk/NMail/Configuration/ObjectConfigurationElement.cs – EkoostikMartin Feb 28 '14 at 22:20

2 Answers2

0

You probably need to include a reference to the actual System.Configuration assembly...

  1. Right click on your project name in the Solution Explorer window
  2. Click Add Reference
  3. Select the .NET tab
  4. Highlight System.Configuration and click OK
Troy Carlson
  • 2,965
  • 19
  • 26
  • I've done this and still didn't work. The "using" directive did not present the error. – La-comadreja Feb 28 '14 at 20:53
  • The `System.Configuration` namespace is used in several assemblies, not just the `System.Configuration` assembly. This means that it may be a valid namespace for some other assembly you may have a reference to. I would double check that you do in fact have a reference to that assembly. – Troy Carlson Feb 28 '14 at 20:55
  • @La-comadreja and if you look into the References node in the Solution Explorer, do you see the "System.Configuration" there? Do you see maybe yellow warning icon next to it? – Csaba Toth Feb 28 '14 at 21:04
  • I see a reference called "System.configuration" (sic, no caps for configuration) and no warning icons. I don't know how to find if it's a warning for another assembly. – La-comadreja Feb 28 '14 at 21:04
  • No caps? Something is whacky there. Also see my question up: what is this `ObjectConfigurationElement`? Do you have only problem with that now, or the `ConfigurationElementCollection` too still? – Csaba Toth Feb 28 '14 at 21:08
  • I've got a problem with both. ObjectConfigurationElement is mentioned in the post referenced in my post. – La-comadreja Feb 28 '14 at 21:10
  • Delete the reference where the C in configuration isn't capitalized and re-add. – Troy Carlson Feb 28 '14 at 21:58
0

What kind of error message? I have a feeling that you cannot compile the source above, and the error message is not a message if an exception from runtime. I assume that the IDE and the IntelliSense warns you. In general, in such cases you should first try the following:

  1. Place the cursor to the class the IDE cannot interpret, in this case for example ConfigurationElementCollection. Then press Ctrl+.. This will offer you the right using statement you need, accept that with an enter.
  2. If the first step doesn't work (such in this case), then it means that you are missing an assembly reference. In this case search MSDN, and determine the needed reference. After that in the Solution Explorer, under the node of your Project right click on References node, Add Reference..., then select Assemblies node in the upcoming Reference Manager, check System.Configuration in this case, then OK. Now go back to the first step, and you get the using with Ctrl+.. If you still don't get the using, then you referenced the wrong assembly.
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • Ctrl+ doesn't seem to load any statements. It just seems to highlight a section of the program. – La-comadreja Feb 28 '14 at 21:12
  • Make sure that your `Target framework:` (right click `Project`, `Properties...`) is `".NET Framework 4"` and not `".NET Framework 4 Client Profile"`. – Csaba Toth Feb 28 '14 at 21:16
  • BTW, I just checked VS 2010, .NET 4, what you mention. I only have problem with ObjectConfigurationElement. Try to start with another clean project and only the first class. Does it work? – Csaba Toth Feb 28 '14 at 21:18
  • I've done what you said @Csaba_Toth. I still get the same problem. No, it doesn't work. – La-comadreja Feb 28 '14 at 21:39
  • Maybe you should approach the problem from the reference, why is it lowercase? That's strange. Right click on that lowercase System.configuration reference item, and select Properties, and look at the path of the actual file. Where is it coming from? On my machine it's C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Configuration.dll, but I have Windows SDK and Debugger Kit and a lot of shebang. – Csaba Toth Mar 01 '14 at 02:06