sampleVariable As Boolean = My.settings.sampleSettings(boolean dataType)不起作用

Car*_*doc 0 vb.net if-statement boolean my.settings visual-studio-2015

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

代码:由于sampleVariable和sampleSettings都是boolean,我就这样声明它们

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()
Run Code Online (Sandbox Code Playgroud)

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

Result: False
1
Result: False
1
Result: False
1
Run Code Online (Sandbox Code Playgroud)

代码:在这段代码中,我没有将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()
Run Code Online (Sandbox Code Playgroud)

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

Result: False
1
Result: True
2
Result: False
1
Run Code Online (Sandbox Code Playgroud)

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

red*_*ted 5

在第一个代码块中,您不会更改设置的值.您只是更改变量的值.

sampleVariable = True
Run Code Online (Sandbox Code Playgroud)

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

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