Question IF statement ?

william7

Member
Joined
Mar 2, 2013
Messages
10
Programming Experience
Beginner
I'm trying to create a lottery application. Below is the code I have so far and the application form. I would like to do this with the most economical IF statement possible. Usin a loop or an array is unacceptable. I realize what I have so far isn't the right way. Can anyone show me the properly constructed IF statement or give me a solid description of it?

VB.NET:
Option Strict On

Public Class Form1

    'Sets Public class variables.
    Shared random As New Random()
    Dim FinalTotalOfTheDay As Double
    Dim intSingleWin As Integer = 2.0

    Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click


        'Sets variables for For Next loops individualized random number generators.
        Dim K As Integer
        For K = 0 To 5
            lbl10.Text = Convert.ToString(random.Next(10, 20))
        Next

        Dim L As Integer
        For L = 5 To 10
            lbl20.Text = Convert.ToString(random.Next(20, 29))
        Next

        Dim M As Integer
        For M = 10 To 15
            lbl30.Text = Convert.ToString(random.Next(30, 39))
        Next

        Dim N As Integer
        For N = 15 To 20
            lbl40.Text = Convert.ToString(random.Next(40, 49))
        Next

        'Sets variables for combo box selected items.
        Dim selectedItem1 = cboOne.SelectedItem.ToString
        Dim selectedItem2 = cboTwo.SelectedItem.ToString
        Dim selectedItem3 = cboThree.SelectedItem.ToString
        Dim selectedItem4 = cboFour.SelectedItem.ToString
       

        If selectedItem1 = lbl10.Text Then

            lbl10.BackColor = Color.DarkGreen
            MsgBox("Winner of " & FormatCurrency(intSingleWin.ToString))

        End If
        

        ' If selectedItem1 = lbl10.Text And selectedItem2 = lbl20.Text  And selectedItem3 = lbl30.Text And selectedItem4 = lbl40.Text Then
        ' lbl10.BackColor = Color.DarkGreen
        'lbl20.BackColor = Color.DarkGreen
        ' lbl30.BackColor = Color.DarkGreen
        'lbl40.BackColor = Color.DarkGreen
        ' MsgBox("JackPot!!")

        ' End If

        If selectedItem2 = lbl20.Text Then
            lbl20.BackColor = Color.DarkGreen
            MsgBox("Winner of " & FormatCurrency(intSingleWin.ToString))


        End If

        If selectedItem3 = lbl30.Text Then
            lbl30.BackColor = Color.DarkGreen
            MsgBox("Winner of " & FormatCurrency(intSingleWin.ToString))


        End If

        If selectedItem4 = lbl40.Text Then
            lbl40.BackColor = Color.DarkGreen
            MsgBox("Winner of " & FormatCurrency(intSingleWin.ToString))


        End If

      


    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Sets variables.
        Dim A As Integer
        Dim B As Integer
        Dim C As Integer
        Dim D As Integer

        'For Next loops for adding numbers to combo boxes.
        For A = 10 To 19

            cboOne.Items.Add(A)


        Next

        For B = 20 To 29

            cboTwo.Items.Add(B)


        Next

        For C = 30 To 39

            cboThree.Items.Add(C)

        Next

        For D = 40 To 49

            cboFour.Items.Add(D)

        Next

        'Sets variables for randomly generated jackpot prize as double.
        Dim myRandom As New Random
        Dim number As Double = (myRandom.NextDouble() * (999.99 - 100.0)) + 100.0
        txtJackpot.Text = number.ToString("C")


    End Sub


    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click

        'Resets label text and combo boxes. 
        cboOne.SelectedIndex = -1
        cboTwo.SelectedIndex = -1
        cboThree.SelectedIndex = -1
        cboFour.SelectedIndex = -1

        lbl10.Text = ""
        lbl20.Text = ""
        lbl30.Text = ""
        lbl40.Text = ""

        lbl10.BackColor = Color.White
        lbl20.BackColor = Color.White
        lbl30.BackColor = Color.White
        lbl40.BackColor = Color.White


    End Sub


    Private Sub btnOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOut.Click

        'Sets variables for combo box selected items.
        Dim SelectedItem1 = cboOne.SelectedItem.ToString
        Dim SelectedItem2 = cboTwo.SelectedItem.ToString
        Dim SelectedItem3 = cboThree.SelectedItem.ToString
        Dim SelectedItem4 = cboFour.SelectedItem.ToString


        If SelectedItem1 = lbl10.Text Then
            intSingleWin += 2
        End If
        If SelectedItem2 = lbl20.Text Then
            intSingleWin += 2
        End If

        If SelectedItem3 = lbl10.Text Then
            intSingleWin += 2
        End If

        If SelectedItem4 = lbl10.Text Then
            intSingleWin += 2
        End If


        FinalTotalOfTheDay += intSingleWin
        MsgBox("Your total winnings are " & FormatCurrency(FinalTotalOfTheDay.ToString))

    End Sub
End Class

5lHEZ3R.jpg
 
Hi,

I would like to do this with the most economical IF statement possible. Usin a loop or an array is unacceptable.

That's fine, but which IF statement would you like us to help you with to make it "more economical"? I can pick one for you if you like?

Please do explain, Very Clearly, what it is you are asking us to help you with.

Cheers,

Ian
 
Yes, pick one or more that will loop through all 4 comboboxes and find single matches, double matches, triple matches and four matches (all four for the jackpot). As you can probably see, I was headed towards producing an extremely long string of IF statements to cover every combination of matches. Any help will be greatly appreciated.
 
Unless you care which number matched, then you only need 4 IFs. Just put the winning numbers in an array and check each user number against it.

pseudo code, not written in VS
VB.NET:
dim count as int = 0

If WinningNumbers().Contains(intMyFirstNum) Then
count = count+1
End If

If WinningNumbers().Contains(intMySecondNum) Then
count = count+1
End If

You can just check the value of count variable to see how many matched.
 
Back
Top