-1

Using a select case write a vb.net program that makes a decision when a driver over-speeds, displays the decision on label changing the color of the label for each decision.

    Dim speed As Integer
    Dim diff As Integer

    'speed limits
    Dim nofine As Integer = 60
    Dim twenty As Integer = 60
    Dim forty As Integer = 80
    Dim sixty As Integer = 100
    Dim last As Integer = 120

    Select Case Val(TextBox1.Text)
        Case 1
            speed = Val(TextBox1.Text)
            diff = speed - nofine
            If diff <= 5 Then
                Label2.Text = "No fine."
            End If
    End Select
End Sub

My problem is I'm failing to show the decision on a label. For each speed limit there is a decision if the driver over-speeds, for example for speed limit - 80MpH the decision is £40 fine and the label2.text should display that decision as well as changing its backcolor to yellow.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • terrible design.. case 1? It means that speed is 1? Cannot understand that code, btw : where is set backGroundColor prop? – Peter Ksenak Oct 17 '19 at 11:20
  • No Peter, case 1 is not speed 1 or may be you want me to change it it to case "80 MpH"? – Takudzwa Mlambo Oct 17 '19 at 11:28
  • case 1 is speed in your code, definitely.. {Case} Val(TextBox1.Text) = {speed =}Val(TextBox1.Text).. it s the same value.. change it to "80" .. btw, as I said terrible design, use enum for declaring speed limit.. otherwise how can u want to write value of fine based on value? It has non sense – Peter Ksenak Oct 17 '19 at 11:38
  • Okay thank you, lemme try that and see the result. – Takudzwa Mlambo Oct 17 '19 at 11:45

1 Answers1

1

I think this is your code

Try it

Public Enum Fines
   twenty = 60
   forty = 80
   sixty = 100
   last = 120
End Enum

Public Class Form1
   Private speed As Integer


   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Int16.TryParse(TextBox1.Text, speed)

    If speed <= Fines.twenty Then
        Label2.Text = "No fine."
    ElseIf speed >= Fines.last Then
        Label2.Text = Fines.last.ToString()
    ElseIf speed >= Fines.sixty Then
        Label2.Text = Fines.sixty.ToString()
    ElseIf speed >= Fines.forty Then
        Label2.Text = Fines.forty.ToString()
    ElseIf speed >= Fines.twenty Then
        Label2.Text = Fines.twenty.ToString()
    End If

End Sub

End Class
Peter Ksenak
  • 305
  • 2
  • 5
  • Think about how to rewrite my if statements into case statement it is your homework from me – Peter Ksenak Oct 17 '19 at 16:02
  • 3
    Can we please stop using Val(). .Net has much better methods (.TryParse comes to mind) for this rather than the old VB6 Val. – Mary Oct 18 '19 at 07:08
  • 1
    just as a note: in it's current format, `Int16.Parse`will throw an exception if `TextBox1.Text` is not in a valid format. Using `.TryParse` as @Mary suggested will prevent this and allow you to easily handle an invalid input case. – AConfusedSimpleton Oct 18 '19 at 08:54
  • Yes, you are right, I also edited my code already, I just thought, that input of textbox should be validated only for integers first.. – Peter Ksenak Oct 18 '19 at 09:45
  • Okay Peter Ksenak, am back with feedback. So i used cases like this; Dim `Speed As Integer = Val(TextBox1.Text) Select Case Speed Case Is < 60 Label2.Text = "Speed out of range".ToString() Label2.BackColor = Color.Aqua() Case Is < 66 Label2.Text = "No fine.".ToString() Label2.BackColor = Color.Green() Case Is < 80 Label2.Text = "£20 fine.".ToString() Label2.BackColor = Color.Yellow() End Select` – Takudzwa Mlambo Oct 24 '19 at 12:34