Boolean属性不会设置为True

时间:2014-01-09 09:22:23

标签: vb.net boolean

我有一个非常基本的表单,字面上有1个标签,文本框和按钮。

单击“确定”按钮后,我需要检查文本框(txtServers)是否为空。如果是,我将属性ServersError设置为True

但是,我用于此的IIF语句无效。然后我直接将ServersError设置为True(正如您在代码中看到的那样),但由于某种原因,当我直接输出结果后,它仍然会返回False

我生气了,或者这里发生了什么奇怪的事情?!感谢。

Public Class formServers

    Private m_ServersError As Boolean = False   ' Whether or not there is an error with the specified servers

    Public Property ServersError() As Boolean
        Get
            ServersError = m_ServersError
        End Get
        Private Set(value As Boolean)
            m_ServersError = ServersError
        End Set
    End Property

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        'Me.ServersError = IIf(Me.txtServers.Text.Length = 0, True, False)
        Me.ServersError = True
        MsgBox(Me.ServersError)
        Me.Hide()
    End Sub

End Class

1 个答案:

答案 0 :(得分:2)

我不是VB人,但不应该这样吗?

Public Class formServers

    Private m_ServersError As Boolean = False   ' Whether or not there is an error with the specified servers

    Public Property ServersError() As Boolean
        Get
            ServersError = m_ServersError
        End Get
        Private Set(value As Boolean)
            m_ServersError = value ' <--------------- changed here
        End Set
    End Property

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        'Me.ServersError = IIf(Me.txtServers.Text.Length = 0, True, False)
        Me.ServersError = True
        MsgBox(Me.ServersError)
        Me.Hide()
    End Sub

End Class