Re: appsetting in class doesn't work
- From: "RobinS" <RobinS@xxxxxxxxxxxxxxx>
- Date: Fri, 17 Nov 2006 22:04:53 -0800
Laziness breeds efficiency? ;-)
Hope it works; let me know.
Robin S.
---------------------------------
"rdufour" <bdufour@xxxxxxxxxx> wrote in message
news:%23hVshoqCHHA.1224@xxxxxxxxxxxxxxxxxxxxxxx
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
"RobinS" <RobinS@xxxxxxxxxxxxxxx> wrote in message
news:BN2dnYjL8sRYmcPYnZ2dnUVZ_vydnZ2d@xxxxxxxxxxxxxx
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.
----------------------------
"Robert Dufour" <bdufour@xxxxxxxxxx> wrote in message
news:Oqy6OGnCHHA.1220@xxxxxxxxxxxxxxxxxxxxxxx
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
"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
.
- 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
- Re: appsetting in class doesn't work
- From: Steve Long
- Re: 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
- Re: appsetting in class doesn't work
- From: RobinS
- Re: appsetting in class doesn't work
- From: rdufour
- appsetting in class doesn't work
- Prev by Date: Re: Creating a form object from string name
- Next by Date: Re: Is my interpretation of COM interface correct?
- Previous by thread: Re: appsetting in class doesn't work
- Next by thread: Re: appsetting in class doesn't work
- Index(es):
Loading