Re: appsetting in class doesn't work
- From: "Steve Long" <Steve_Noneya@xxxxxxxxxx>
- Date: Thu, 16 Nov 2006 12:34:08 -0800
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
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:ODxkS1bCHHA.1012@xxxxxxxxxxxxxxxxxxxxxxx
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
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:esZh6WbCHHA.3524@xxxxxxxxxxxxxxxxxxxxxxx
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
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:eGfK2daCHHA.1012@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- References:
- appsetting in class doesn't work
- From: Robert Dufour
- Re: appsetting in class doesn't work
- From: Steve Long
- Re: appsetting in class doesn't work
- From: Robert Dufour
- appsetting in class doesn't work
- Prev by Date: Re: appsetting in class doesn't work
- Next by Date: Dynamice Report Option Screen
- Previous by thread: Re: appsetting in class doesn't work
- Next by thread: Re: appsetting in class doesn't work
- Index(es):
Loading