Re: appsetting in class doesn't work



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
"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:%23AayAadCHHA.3924@xxxxxxxxxxxxxxxxxxxxxxx
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

"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:uNKLrDdCHHA.3448@xxxxxxxxxxxxxxxxxxxxxxx
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







"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:OpqQS6bCHHA.4928@xxxxxxxxxxxxxxxxxxxxxxx
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














.