在课堂上appsetting不起作用 [英] appsetting in class doesn't work
问题描述
这里是类代码片段
导入System.Configuration.ConfigurationManager
公共类Class1
Public _strTestSetting As String
Public Sub SetTestsetting()
_strTestSetting = AppSettings.Get(" TestSetting") - 应该得到价值
应用程序配置文件中的TestSetting
结束次级
结束班级
这是项目中调用类的项目片段--Project TestIt - 要进行
测试,你应该有一个名为TestSetting的设置应用范围,值
Testvalue
导入System.Configuration
公共类Form1
私有子Button1_Click(ByVal发送者为System.Object,ByVal e as
System.EventArgs)处理Button1.Click
Dim mycls As New clsTestconfig.Class1
mycls .SetTestsetting()
Label1.Text = mycls._strTestSetting
结束子
结束班
项目Testit的设置文件中设置了TestSetting
在线设置断点
_strTestSetting = AppSettings.Get(" TestSetting")
单击按钮,当您跨越断点时,您会看到_strTestSetting的
值保持不变。也就是说,它没有在app.config中获取TestSetting的价值
。
我怎样才能获得这个价值的提升?
感谢您的帮助
Bob
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
推荐答案
罗伯特,这个是我第一次涉足.NET 2.0的配置系统和
这对我来说似乎非常困难但是这里我是如何获得你的
项目的价值的正试图检索。
首先是配置文件:
< appSettings>
< add key = QUOT; TestSetting" value =" TestValue" />
< / appSettings>
现在代码:
Dim s As String
Dim cnfg As System.Configuration.Configuration
Dim el As KeyValueConfigurationElement
cnfg =
System.Configuration.ConfigurationManager.OpenExeC onfiguration(ConfigurationUserLevel.None)
el = cnfg.AppSettings.Settings.Item (TestSetting)
s = el.Value.ToString()
这确实会返回我拥有的值TestValue ;在配置
文件中。
HTH
S
PS在VS2003中似乎要容易得多
" Robert Dufour" < bd ***** @ sgiims.com写信息
新闻:eG ************** @ TK2MSFTNGP04.phx.gbl ...
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.OpenExeC onfiguration(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" <bd*****@sgiims.comwrote in message
news:eG**************@TK2MSFTNGP04.phx.gbl...
这里是类代码片段
Imports System.Configuration.ConfigurationManager
公共类Class1
Public _strTestSetting As String
Public Sub SetTestsetting()
_strTestSetting = AppSettings。获取(TestSetting) - 应该获得值
appSetting in app config file
End Sub
结束类
这是项目中调用类的项目片段--Project TestIt - 要进行
测试,你应该有一个名为TestSetting应用程序范围的设置,价值
Testvalue
进口System.Configuration
公共舱Form1
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim mycls As New clsTestconfig.Class1
mycls.SetTestsetting()
Label1.Text = mycls._strTestSetting
>
结束次级
结束班级
在项目Testit的设置文件中设置TestSetting
在线设置断点
_strTestSetting = AppSettings.Get(" TestSetting")
点击按钮,当你跨过断点时,你会看到_strTestSetting的
值保持不变。也就是说,它没有在app.config中获取TestSetting的
值。
如何才能获得该值的提升?
感谢您的帮助
Bob
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
谢谢史蒂夫我要测试一下,是的,为什么当你这么简单的时候让事情变得简单?
可以让它们复杂化:-(
鲍勃
Steve Long< St ********** @ NoSpam.com写信息
news:es *** *********** @ TK2MSFTNGP06.phx.gbl ...
Thanks Steve I''m gonna test it out and yes, why make things simple when you
can make them complicated :-(
Bob
"Steve Long" <St**********@NoSpam.comwrote in message
news:es**************@TK2MSFTNGP06.phx.gbl...
Robert,这是我第一次涉足.NET的配置系统2.0
对我来说这看起来非常困难但是这里我是如何获得你想要检索的物品的价值
。
首先是配置文件:
< appSettings>
< add key =" TestSetting" value =" TestValue" />
< / appSetting s>
现在代码:
Dim s As String
Dim cnfg As System .Configuration.Configuration
Dim el As KeyValueConfigurationElement
cnfg =
System.Configuration.ConfigurationManager.OpenExeC onfiguration (ConfigurationUserLevel.None)
el = cnfg.AppSettings.Settings.Item(" TestSetting")
s = el.Value.ToString ()
这确实返回了我拥有的值TestValue在配置
文件中。
HTH
S
PS在VS2003中似乎要容易得多
" Robert Dufour" < bd ***** @ sgiims.com写信息
新闻:eG ************** @ TK2MSFTNGP04.phx.gbl ...
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.OpenExeC onfiguration(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" <bd*****@sgiims.comwrote in message
news:eG**************@TK2MSFTNGP04.phx.gbl...
>这里是类代码片段
Imports System.Configuration.ConfigurationManager
Public Class Class1
<公共_strTestSetting As String
公共子SetTestsetting()
_strTestSetting = AppSettings.Get(" TestSetting") - 应该得到值
TestSetting在应用程序配置文件中
结束子
结束类
这是调用类的项目片段--Project TestIt - To
测试你应该有一个名为TestSetting的设置应用范围,值
Testvalue
Imports System.Configuration
公共类Form1
私人Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim mycls As new clsTestconfig.Class1
mycls。 SetTestsetting()
Label1.T ext = mycls._strTestSetting
End Sub
结束类
在项目Testit的设置文件中设置TestSetting >
在线设置断点
_strTestSetting = AppSettings.Get(" TestSetting")
点击按钮,当你跳过断点时,你会看到_strTestSetting的
值保持不变。也就是说,它没有在app.config中获取TestSetting的值。
我怎样才能获得价值的提升?
感谢任何人帮助
鲍勃
>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 = el.Value.ToString()对象引用未设置为
$的实例b $ b对象。多数民众赞成因为行了
el = cnfg.AppSettings.Settings.Item(" TestSetting")
el什么都不做,这无疑是因为我的app.config文件正在使用
由VS2005中的设置设计器生成的新格式,后面是
。
< ; applicationSettings>
< TestClassApConfig.My.MySettings>
< setting name =" TestSetting" serializeAs =" String">
< value> Testvalue< / value>
< / setting>
< /TestClassApConfig.My.MySettings>
< / applicationSettings>
确实当我使用旧式app.config文件测试它
< appSettings>
< add key =" TestSetting" value =" TestValue" />
< / appSettings>
代码工作正常。
现在的问题是,怎么做当你使用VS2005的内置工具创建你的应用程序配置文件时,你检索TestSetting值 - 使用add item-new item创建一个
新的app.config文件 - 应用程序配置和
将应用程序范围设置放在文件中,使用Myproject -
设置在IDE中进行开发时写入设置。
感谢您的帮助,
Bob
" Steve Long" < St ********** @ NoSpam.comwrote in message
news:es ************** @ TK2MSFTNGP06.phx.gbl ...
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" <St**********@NoSpam.comwrote in message
news:es**************@TK2MSFTNGP06.phx.gbl...
罗伯特,这是我第一次涉足.NET 2.0的配置系统
,这对我来说似乎非常困难但是这是我如何获得你想要检索的项目的价值
。
首先是配置文件:
< appSettings>
< add key =" TestSetting" value =" TestValue" />
< / appSettings>
现在代码:
Dim s As String
Dim cnfg As System.Configuration.Configuration
Dim el As KeyValueConfigurationElement
cnfg =
System.Configuration.ConfigurationManager.OpenExeC onfiguration(ConfigurationUserLevel.None)
el = cnfg.AppSettings.Settings.Item (TestSetting)
s = el.Value.ToString()
这确实会返回我拥有的值TestValue ;在配置
文件中。
HTH
S
PS在VS2003中似乎要容易得多
" Robert Dufour" < bd ***** @ sgiims.com写信息
新闻:eG ************** @ TK2MSFTNGP04.phx.gbl ...
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.OpenExeC onfiguration(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" <bd*****@sgiims.comwrote in message
news:eG**************@TK2MSFTNGP04.phx.gbl...
>这里是类代码片段
Imports System.Configuration.ConfigurationManager
Public Class Class1
<公共_strTestSetting As String
公共子SetTestsetting()
_strTestSetting = AppSettings.Get(" TestSetting") - 应该得到值
TestSetting在应用程序配置文件中
结束子
结束类
这是调用类的项目片段--Project TestIt - To
测试你应该有一个名为TestSetting的设置应用范围,值
Testvalue
Imports System.Configuration
公共类Form1
私人Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim mycls As new clsTestconfig.Class1
mycls。 SetTestsetting()
Label1.T ext = mycls._strTestSetting
End Sub
结束类
在项目Testit的设置文件中设置TestSetting >
在线设置断点
_strTestSetting = AppSettings.Get(" TestSetting")
点击按钮,当你跳过断点时,你会看到_strTestSetting的
值保持不变。也就是说,它没有在app.config中获取TestSetting的值。
我怎样才能获得价值的提升?
感谢任何人帮助
Bob
>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
这篇关于在课堂上appsetting不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!