appsetting in class doesn't work

R

Robert Dufour

Here's the class code snippet
Imports System.Configuration.ConfigurationManager

Public Class Class1

Public _strTestSetting As String



Public Sub SetTestsetting()

_strTestSetting = AppSettings.Get("TestSetting") -should get the value of
TestSetting in app config file

End Sub


End Class

This is the snippet in the project calling the class -Project TestIt - To
test you should have a setting named TestSetting application scope, value
Testvalue

Imports System.Configuration

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim mycls As New clsTestconfig.Class1

mycls.SetTestsetting()

Label1.Text = mycls._strTestSetting

End Sub

End Class

There's a setting TestSetting in the settings file of the project Testit

Put a breakpoint on line

_strTestSetting = AppSettings.Get("TestSetting")

Click the button, you will see as you step over the breakpoint that the
value of _strTestSetting stays at nothing. ie, its not picking up the value
of TestSetting in app.config.

How can I get this pickup of the value to work?

Thanks for any help

Bob
 
S

Steve Long

Robert, this is my first foray into the configuration system of .NET 2.0 and
it seem ridiculously difficult to me but here's how I got the value of the
item that you are trying to retrieve.
First the config file:

<appSettings>

<add key="TestSetting" value="TestValue"/>

</appSettings>

Now the code:

Dim s As String

Dim cnfg As System.Configuration.Configuration

Dim el As KeyValueConfigurationElement

cnfg =
System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

el = cnfg.AppSettings.Settings.Item("TestSetting")

s = el.Value.ToString()

This does indeed return the value that I have "TestValue" in the config
file.

HTH

S

P.S. It seems much easier in VS2003
 
R

Robert Dufour

Thanks Steve I'm gonna test it out and yes, why make things simple when you
can make them complicated :-(

Bob
 
R

Robert Dufour

Hi steve,
When running it I'm getting an unhandled exception error on line
s = el.Value.ToString() Object reference not set to an instance of an
object. Thats because with the line
el = cnfg.AppSettings.Settings.Item("TestSetting")

el stays at nothing and thats no doubt because my app.config file is using
the new format generated by the settings designer in VS2005 which is as
follows.

<applicationSettings>

<TestClassApConfig.My.MySettings>

<setting name="TestSetting" serializeAs="String">

<value>Testvalue</value>

</setting>

</TestClassApConfig.My.MySettings>

</applicationSettings>

And indeed when I tested it using the old style app.config file
<appSettings>
<add key="TestSetting" value="TestValue"/>
</appSettings>
the code works fine.
The question now becomes, how do you retrieve the TestSetting value when you
created your app config file using the built in tools of VS2005 - creating a
new app.config file with add item-new item- application configuration and
putting application scope settings in the file by using the Myproject -
settings to write settings while developping in the IDE.

Thanks for your help,

Bob
 
S

Steve Long

Well isn't that nice of MS to take the confusion to, yet, another level...
:)
You know, you might be able to get at those setting via the My.Settings
functionality.

Alternatively here's a microcosm of my app.config file, which I included by
right cliking on my project and clicking add/ New Item and selecting the
Application Configuration item.

<configuration>

<appSettings>

<add key="TestSetting" value="TestValue"/>

</appSettings>

</configuration>

Check it out

S
 
R

Robert Dufour

I tried via My.settings can't seem to get to it. :-(
Jeez has anyone at MS ever heard of KISS? Like in Keep It Simple Stupid.

I'm starting to think the only way to get this project off the ground and
working is to go back to Visual Studio 2003 or even Vs 6.

Hope someone can come up with a way to get to these values in the new XML
format being used by the app.config files in Vs2005.

Thanks
Bob
 
S

Steve Long

Hi Robert. Please forgive me if I'm making assumptions here but it sort of
sounds like there's a difference here between the app.config file and the
settings enter in the designer. It looks to me, (remember I have little
experience with this), that this whole thing can get easily mucked up. So, I
started a new project, click on the project in the in the solution
expolorer, clicked properties, clicked settings, enter the "TestSetting"
setting and gave it a default value of "TestValue", then in the Form Load
event, I just did this:
Dim s As String = My.Setting.TestSetting
and it returned the TestValue value.

That was it.
Steve
 
R

RobinS

That's true. The settings are project-specific.
Robin S.
----------------------------------
 
R

Robert Dufour

Yes Steve, that works but if you add a class project to your solution
(clsProject2) and that class has no form, but its called from your form in
Project1 where you have a form and then you want some code in clsProject2 to
obtain values from the appconfig file of Project1, in that case code in
clsProject2 can't get values using the my.settings. I have two projects in
the solution and I am trying to obtain settings from the app.config file of
project1 by using code in clsProject2. I could do that in Vs2003 and I can
do it in Vs2005 using the code that you sent me yesterday PROVIDING the
applicationsettings are in the xml format that was used in Vs2003. But now
the VS2005 IDE when it creates settings creates a different syntax for
settings. Its no longer in a section <appsetings> with the tags <addkey> it
is now using a wholly different set of XML tags.

<applicationSettings>
<TestClassApConfig.My.MySettings>
<setting name="TestSetting" serializeAs="String">
<value>Testvalue</value>
etc...
You can get these quite simply using the my.settings IF that is in the code
in the same project as the app.config file but I can't find a way to get
these values if my code is in a second project in the solution AND if the
config file uses the XML format standard of Vs2005.

Bob
 
R

Robert Dufour

Yes Robin,
However how do you obtain config setttings from within classes that are
being reused with the new application settings XML format. The my.settings
can not get settings from the main project if it is used in a second class
project in the application. There must be a way to do this though. It was
doable in Vs2003 using the 2003 XML format for the app.config file, but in
2005 the format was changed and the technique used in 2003 no longer works
with the 2005 XML app.config format.

Bob
 
R

Robert Dufour

Ok guys thanks to you both I found a way for now, far from elegant but it
works at least in my test app.
This is the typical full app.config file in Vs2005. See the bottom I added
an appsettings section.
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

<section name="TestClassApConfig.My.MySettings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>

</sectionGroup>

</configSections>

<system.diagnostics>

<sources>

<!-- This section defines the logging configuration for
My.Application.Log -->

<source name="DefaultSource" switchName="DefaultSwitch">

<listeners>

<add name="FileLog"/>

<!-- Uncomment the below section to write to the Application Event Log -->

<!--<add name="EventLog"/>-->

</listeners>

</source>

</sources>

<switches>

<add name="DefaultSwitch" value="Information" />

</switches>

<sharedListeners>

<add name="FileLog"

type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"

initializeData="FileLogWriter"/>

<!-- Uncomment the below section and replace APPLICATION_NAME with the name
of your application to write to the Application Event Log -->

<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener"
initializeData="APPLICATION_NAME"/> -->

</sharedListeners>

</system.diagnostics>

<applicationSettings>

<TestClassApConfig.My.MySettings>

<setting name="TestSetting" serializeAs="String">

<value>Testvalue1</value>

</setting>

</TestClassApConfig.My.MySettings>

</applicationSettings>

<appSettings>

<add key="TestSetting" value="TestValue"/>

</appSettings>

</configuration>

In the class project, I use the code that Steve gave me. Class code snippet
is below

Imports System.Configuration.ConfigurationManager

Imports System.Configuration

Public Class Class1

Public _strTestSetting As String

Public Sub SetTestsetting()

Dim s As String

Dim cnfg As System.Configuration.Configuration

Dim el As KeyValueConfigurationElement

cnfg =
System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

el = cnfg.AppSettings.Settings.Item("TestSetting")

s = el.Value.ToString()

_strTestSetting = s

End Sub

End Class

This code used in the second class project picks up the values in the
app.config file of the startup project in the section appsettings. In the
startup project I can use My.settings("TestSetting") to pick up the value in
the applicationSettings section. Its a bit of a kludge and it means I will
have to make sure the values in the two sections are synchronized, but its
workable until I find a way to get rid of the appSettings section in the
app.config file and use only the section.

<TestClassApConfig.My.MySettings>

<setting name="TestSetting" serializeAs="String">

<value>Testvalue1</value>

</setting>

</TestClassApConfig.My.MySettings>

Which is the new XML format in Vs2005

Bob
 
R

RobinS

Ugh.That's a lot of work. I'd probably try to figure out
a way to put a public property in the project containing
the settings I wanted, and have that expose the values
to the other classes.

Congrats on finding a solution!

Robin S.
----------------------------
 
S

Steve Long

You can also pass around a SettingsContext object but I cannot figure out
how to use that darn thing.
In this object, there is the "ProjectName.My.Settings" key but I'm having a
hard time getting at it.

Uhg. Shouldn't this be easier than this?
Theoretically, you should be able to get down to the setting that you want
through this SettingsContext object. Anybody???

S
 
S

Steve Long

Robert, if you read this article:
http://visualbasic.about.com/od/usingvbnet/a/appsettings.htm
I think it may make you want to hurl at what MS has done with configuration
files in general.
I created an assembly for use in my applications to handle application
settings. It uses the tag format of VS2003 and allows for multiple sections.
The main difference in this assembly is that you pass in the name of the
section along with the name of the key to get the value for said key.
Additionally, at a later time, I realized that I needed the capability to
create these config files on the fly, since that time, there was a second
class placed in the assembly CAppInit2 that will do just that.
Other than that, it works pretty much like the old config files worked. But,
you pass in the pathFileName to load a configuration file (a bit of freedom
right there), and, you can change a config file, while the app is running
(outside of the app), and this assembly will pickup those changes and reload
itself. Let me know if you want the code, I'd be happy to share as this
stuff that MS has done just looks horrific to me.

S
 
R

rdufour

That's a heck of an idea, now why didn't I think of that:).
That way I can use the my.settings feature to set the properties on startup
of the app and not have to maintain two sections in the app.config file.
I'm going to test that tomorrow.

Thanks,
Bob
 
R

rdufour

I'm going to control myself in commenting on MS <GGG>.
In 2003 I could easily change the config file settings from inside the app,
so I could have an application that on start would test its connection
string for instance and if it could not connect it would bring up a
configuration string modification and testing screen, so if the admin
changed the server name, no problem, easy to fix. In 2005 standard
application scope settings can't be changed. Nice going. MS just decided
that you should not do that. Now we gotta play in the config files with
notepad, Thanks a lot and it just gets better as you go along.

Anyways, yes Steve I would appreciate a copy of the assemblies you made, I
will surely try to use them.

Bob
 
R

RobinS

Laziness breeds efficiency? ;-)
Hope it works; let me know.
Robin S.
---------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top