case语句为true,但不执行true case条件下的代码

时间:2017-07-30 17:03:39

标签: vb.net select-case

我有一个应用程序,其中有组合框。在这些组合框中,我希望自动填充单位。我已经构建了一个有select..case条件的代码。 case语句为true,但不执行true case语句后面的代码。我已经通过断点对它进行了分析,它也显示了它True但它没有执行并跳转到最后。请指教。

Private Sub Load_units_combo_boxes()

    Dim ComboBoxes As New ArrayList

    For Each ComboBox In Me.Controls
        If ComboBox.Name.Contains("Units") And TypeOf ComboBox Is ComboBox Then
            ComboBoxes.Add(ComboBox)
        End If
    Next

    For Each ComboBox In ComboBoxes
        Select Case ComboBox.name.ToString
            Case ComboBox.name.ToString.Contains("Pressure")
                For Each unit In _units
                    If unit.contains("Pressure") Then
                        unit.replace("Pressure", "")
                        ComboBox.items.add(unit)
                    End If
                Next

            Case ComboBox.name.ToString.Contains("Distance")
                For Each unit In _units
                    If unit.contains("Distance") Then
                        unit.replace("Distance", "")
                        ComboBox.items.add(unit)
                    End If
                Next
        End Select
    Next

End Sub

Public _units As New ArrayList

Private Sub units_collection()

    Dim units As New Units_and_conversions
    Dim properties = units.GetType().GetProperties()

    For Each prop In properties
        _units.Add(prop.Name)
    Next


End Sub

Public Class Units_and_conversions

Private universal_distance_unit As Double = 1
Private universal_pressure_unite As Double = 1


Public Property Distance_Meter() As Double
    Get
        Return universal_distance_unit
    End Get
    Set(value As Double)
        value = universal_distance_unit
    End Set
End Property

Public Property Distance_MilliMeter() As Double

    Get
        Return 0.001 * universal_distance_unit
    End Get
    Set(value As Double)
        value = 0.001 * universal_distance_unit

    End Set
End Property

结束班

1 个答案:

答案 0 :(得分:0)

此代码失败。

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
 Button1.Click

    Dim s As String = "abcdefghijklmnopqrstuvwxyz"

    Select Case s
        Case s.Contains("g")
            MsgBox("Case Select Fails")
    End Select

    If s.Contains("g") Then
        MsgBox("If statement works")
    End If

End Sub

"案例选择"未设置为布尔问题,因此失败。以下代码将起作用。

此代码正常工作

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
  Button1.Click

    Dim s As String = "abcdefghijklmnopqrstuvwxyz"

    Select Case True
        Case s.Contains("g")
            MsgBox("Case Select Works")
    End Select

    If s.Contains("g") Then
        MsgBox("If statement works")
    End If
End Sub

此代码有效,因为问题可以正确设置为布尔问题,而不是字符串比较。