Hello

I'm using SELECT CASE statement for age cases
but I've two inputs years and months

Case 0 To 1
                MS_lbl_category.Text = CStr("infant")
            Case 1 To 2
                MS_lbl_category.Text = CStr("toddlerI")
            Case 2 To 6
                MS_lbl_category.Text = CStr("kindergarten")
            Case 6 To 12
                MS_lbl_category.Text = CStr("child")
            Case 12 To 19
                MS_lbl_category.Text = CStr("teenager")
            Case 19 To 25
                MS_lbl_category.Text = CStr("young adult")
            Case 25 To 40
                MS_lbl_category.Text = CStr("adult")
            Case 40 To 60
                MS_lbl_category.Text = CStr("middle aged")
            Case 60 To 120
                MS_lbl_category.Text = CStr("senior citizen")
            Case Else
                MS_lbl_category.Text = CStr("Are you kidding ?")

when the input is 12 years and 6 months I want the program to print teenager
I'm not sure how to do that

and whenever I leave the months text box empty I need the program to just read the year without crashing

when the input is 12 years and 6 months I want the program to print teenager
I'm not sure how to do that

Input should be 12.6 to get right result.
so you can concatenate textbox1 (for year) and textbox2 (for month) :

Private Sub Command1_Click()
Select Case Val(Text1.Text) & "." & Val(Text2.Text)
    Case 0 To 1
        MS_lbl_category.Text = CStr("infant")
    Case 1 To 2
        MS_lbl_category.Text = CStr("toddlerI")
    Case 2 To 6
        MS_lbl_category.Text = CStr("kindergarten")
    Case 6 To 12
        MS_lbl_category.Text = CStr("child")
    Case 12 To 19
        MS_lbl_category.Text = CStr("teenager")
    Case 19 To 25
        MS_lbl_category.Text = CStr("young adult")
    Case 25 To 40
        MS_lbl_category.Text = CStr("adult")
    Case 40 To 60
        MS_lbl_category.Text = CStr("middle aged")
    Case 60 To 120
        MS_lbl_category.Text = CStr("senior citizen")
    Case Else
        MS_lbl_category.Text = CStr("Are you kidding ?")
End Select
End Sub

Close, @Jx_Man, but no cigar. The issue with your solution is that your "Select Case" statement on line 2 causes a string comparison rather than a numeric comparison. So your "12.6" example categorizes into "Case 1 to 2" (test it!). Your line 2 should read:

Select Case Val(Val(Text1.Text) & "." & Val(Text2.Text))

That will force the comparison to be numeric. Of course, you'd have to have some sort of edit above the Case statement to ensure that you're getting numbers in the first place.

However, that's not the largest problem with the whole scenario.

To the Original Poster: this kind of case statement is a common error in range evaluation. What happens with your boundary conditions? If the value was (for instance) exactly 12, should that be a child or a teenager? The way you've specified the ranges, you have uncertainty.

As an alternative, you can take advantage of the behavior of the VB Case statement to have it cascade like so:

Select Case Val(Val(Text1.Text) & "." & Val(Text2.Text))
    Case Is < 0
        MS_lbl_category.Text = CStr("Hasn't been born yet!")
    Case Is < 1
        MS_lbl_category.Text = CStr("infant")
    Case Is < 2
        MS_lbl_category.Text = CStr("toddlerI")
    Case Is < 6
        MS_lbl_category.Text = CStr("kindergarten")
    Case Is < 12
        MS_lbl_category.Text = CStr("child")
    Case Is < 19
        MS_lbl_category.Text = CStr("teenager")
    Case Is < 25
        MS_lbl_category.Text = CStr("young adult")
    Case Is < 40
        MS_lbl_category.Text = CStr("adult")
    Case Is < 60
        MS_lbl_category.Text = CStr("middle aged")
    Case Is < 120
        MS_lbl_category.Text = CStr("senior citizen")
    Case Else
        MS_lbl_category.Text = CStr("Are you kidding ?")
End Select

The disadvantage with this technique is that it requires you to define the entire number system as your range (including negative numbers!). With clever edits above it, you could avoid that. However, there are no gaps and therefore no uncertainty.

Hope this helps! Good luck!

commented: Yeah...completely right..Misiing it.. :) +14

First of all, you're to be commended for following the guidelines and submitting your code along with the request. The ability to follow instructions is a good indicator of future success!

when the input is 12 years and 6 months I want the program to print teenager
I'm not sure how to do that

BitBlt has a good suggesting of converting the test to a numeric value. You should do the same with the months value.
Use an if statement:

if (months = 6)
     Printer.print "teenager"
End If

and whenever I leave the months text box empty I need the program to just read the year without crashing

Use a test before submitting the value to your select case structure: if txtMonths.text = vbnullString then txtMonths.text = 0

Thanks guys :)

Private Sub MS_btn_calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MS_btn_calculate.Click
        Dim y As Integer = CDbl(Val(Val(MS_txt_years.Text)) & "." & Val(Val(MS_txt_months.Text)))

        If MS_txt_months.Text = vbNullString Then MS_txt_months.Text = "0"


        Select Case y
            Case Is <= 1
                MS_lbl_category.Text = CStr("infant --" & y)
            Case Is <= 2
                MS_lbl_category.Text = CStr("toddlerI --" & y)
            Case Is <= 6
                MS_lbl_category.Text = CStr("kindergarten --" & y)
            Case Is <= 12
                MS_lbl_category.Text = CStr("child --" & y)
            Case Is <= 19
                MS_lbl_category.Text = CStr("teenager --" & y)
            Case Is <= 25
                MS_lbl_category.Text = CStr("young adult --" & y)
            Case Is <= 40
                MS_lbl_category.Text = CStr("adult --" & y)
            Case Is <= 60
                MS_lbl_category.Text = CStr("middle aged --" & y)
            Case Is <= 120
                MS_lbl_category.Text = CStr("senior citizen --" & y)
            Case Else
                MS_lbl_category.Text = CStr("Are you kidding ? --" & y)
        End Select
       
    End Sub
commented: Upvote for posting your eventual solution -- Thanks! +8

However, that's not the largest problem with the whole scenario.

To the Original Poster: this kind of case statement is a common error in range evaluation. What happens with your boundary conditions? If the value was (for instance) exactly 12, should that be a child or a teenager? The way you've specified the ranges, you have uncertainty.

I don't think there's any logical uncertainty (the case statement will execute the same way every time). At age 12 it would always be a 'Child' since that is the first branch to be executed in the compiled machine code. Although I suppose there is some uncertainty when someone reads the code...

But the uncertainty, to me anyway, that you are talking about is like applying a voltage to the 'indeterminate zone' of a TTL input whereas you literally have no idea what the outcome will be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.