0

I have an app where there are combo boxes. In these combo boxes I want units to populated automatically. I have built a code where there is select..case condition. The case statement is true but the code following the true case statement is not executed. I have analyzed it through breakpoints, and it also shows it True but it is not executed and jumps to the end. Please advise.

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

End Class

  • 2
    That will not even compile under `Option Strict`. The test, `ComboBox.name.ToString.Contains("Pressure")` will result in True or False. The code is trying to compare that to the control name. No need to use `ToString()` on a string property. – Ňɏssa Pøngjǣrdenlarp Jul 30 '17 at 17:06
  • 2
    [Select case true](https://stackoverflow.com/q/794036/11683) is what you meant. – GSerg Jul 30 '17 at 17:12
  • if I do not use ToString it gives error and do not compile. – neutralizer Jul 30 '17 at 17:14
  • If you edited your question to include what `_units` is (or a useful small sample of it) then it is likely that we will be able to suggest something for you. – Andrew Morton Jul 30 '17 at 17:31
  • Also, there are some ready-made units packages. I haven't used any, but consulting your favourite search engine for ".net units" may give useful results. – Andrew Morton Jul 30 '17 at 17:35
  • And another also... it's generally a Bad Idea to give variables the same name (say, ComboBox) as a class. `cb` would suffice, given the context. – Andrew Morton Jul 30 '17 at 17:38
  • 1
    You are confusing a `Case` statement with an `If` statement. The `If` statement checks for `True`. The `Case` statement checks for equality with the corresponding `Select`. – Raymond Chen Jul 30 '17 at 17:42
  • I added _units collection and details. Your help is very much appreciated. – neutralizer Jul 30 '17 at 17:43

1 Answers1

0

This code fails.

 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

The "Case Select" is not setup as a Boolean question so it fails. The following code will work.

This code Works

 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

This code works because the question is setup properly as a Boolean question and not a string compare.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Jimva
  • 36
  • 6