0

我有两个场景涉及使用My.Settings.sampleSettingsadataType boolean和 a sampleVariableas data type boolean

代码:由于 sampleVariable 和 sampleSettings 都是布尔值,我这样声明它们

Dim sampleVariable As Boolean = My.Settings.sampleSettings
Console.WriteLine("Result: " & sampleVariable)
If sampleVariable = False Then
    Console.WriteLine("1")
    sampleVariable = True
Else
    Console.WriteLine("2")
    sampleVariable = False
End If
My.Settings.Save()

输出:输出似乎不满足条件1,它总是满足条件2,即为假

Result: False
1
Result: False
1
Result: False
1

代码:在这段代码中,我没有将 sampleSettings 放入布尔变量,它工作正常。

Console.WriteLine("Result: " & My.Settings.sampleSettings)
If My.Settings.sampleSettings = False Then
    Console.WriteLine("1")
    My.Settings.sampleSettings = True
Else
    Console.WriteLine("2")
    My.Settings.sampleSettings = False
End If
My.Settings.Save()

输出:每当我单击按钮时,它都会触发不同的条件,这就是我的目标。

Result: False
1
Result: True
2
Result: False
1

问题:如何正确地将 My.Settings.sampleSettings 包含到布尔变量中?

4

1 回答 1

5

在第一个代码块中,您没有更改设置的值。您只是在更改变量的值。

sampleVariable = True

这只会改变 的值sampleVariable。它不会改变 的值My.Settings.sampleSettings

在第二个代码块中,您正在更改 的值My.Settings.sampleSettings,这就是保存该值的原因。

于 2016-02-27T01:09:01.757 回答