如何使用 vb.net 的 my.settings 名称和值执行 for 循环? [英] How can i do a for loop with vb.net's my.settings name and value?
问题描述
我需要一些帮助,我试过在谷歌上搜索,也试过在这里搜索但没有成功.
i would like some help with this, i have tried searching on google and have tried searching here with no luck.
到目前为止我尝试过的内容如下:-
what i have tried so far is the following:-
For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
Dim myrecord As String = My.Settings.PropertyValues.ToString
sql_all_matching_records = String.Format("")
Next
但是当我在调试器模式下运行并在 for 循环中放置一个断点时,这会被跳过.
but this just gets skipped when i run in debugger mode and have put a break point within the for loop.
我想要做的是能够遍历 my.settings 中的变量名称,然后获取其值并将其与 SQL 查找进行比较.我唯一挣扎的是遍历 my.settings.
what i want to do is be able to loop through the variable names in my.settings and then take its value and compare it to an SQL lookup. the only thing i am struggling with is looping through the my.settings.
ABOVE 已经得到回答,但是在图片中出现错误,是否写错了代码?不知道我明白什么 vb.net 试图告诉我.我认为这是正确的.
ABOVE has been answered, however getting the error in picture, have a wrote the code wrong? not sure i understand what vb.net is trying to tell me. i thought it was correct.
推荐答案
有点免责声明,我不熟悉如何/何时加载设置以及 PropertyValues 集合被填充,但它似乎不成立直到访问设置值.这是一个奇怪的解决方法,但在循环遍历集合之前从设置中获取一个值似乎可以填充它.您的 InvalidCast 异常发生是因为您试图设置 myrecord = a collection.tostring.我们已经知道集合有多个值,因为我们正在迭代它们,所以我们不能只调用 .tostring .这就像说 Array.ToString(),只是不能那样工作.我认为您实际上正在寻找的是 value.PropertyValue.ToString,它将保存您想要获得的设置值.应用这两项更改后,您会得到:
A bit of a disclaimer, I'm not familiar with how/when settings are loaded and the PropertyValues collection gets filled, but it doesn't seem to hold anything until a settings value is accessed. It's a wierd workaround, but grabbing a value from settings before looping through the collection seems to fill it. Your InvalidCast exception occurs because you're trying to set myrecord = a collection.tostring. We already know that the collection has multiple values because we're iterating them, so we can't just call .tostring on it. That's like saying Array.ToString(), just doesn't work that way. I think what you're actually looking for is value.PropertyValue.ToString, that will hold the settings value you're trying to get. With both those changes applied, you get this:
Dim dummy As String = My.Settings.dummySetting
For Each value As System.Configuration.SettingsPropertyValue In My.Settings.PropertyValues
'Dim myrecord As String = My.Settings.PropertyValues.ToString
Dim myrecord As String = value.PropertyValue.ToString
sql_all_matching_records = String.Format("")
Next
dummySetting 只是我在设置中调用的一个空值.希望这有帮助
The dummySetting is just an empty value that I put in settings to call on. Hope this helps
这篇关于如何使用 vb.net 的 my.settings 名称和值执行 for 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!