#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    IF statement calculation ?


    Can anybody give me an idea how to fashion an economical IF statement that will provide for single wins $2.00, double wins $10.00, Half the jackpot, and full jackpot wins for the lottery application below? I realize I could produce a very lengthy piece of code that will cover every conceivable combination of winning, but if there's a shorter way I would rather take it instead.

    So far, my code is written for single wins (two matches) and the full jackpot (four matches). Double wins, any combination of two matches, and half the jackpot win which is any combination of three matches will take a lengthy IF statement.

    I'm new to VB and programming and in bad need of help on this one.

    Code:
    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(SingleWin.ToString))  
      
            End If  
             
      
            If selectedItem2 = lbl20.Text Then  
                lbl20.BackColor = Color.DarkGreen  
                MsgBox("Winner of " & FormatCurrency(SingleWin.ToString))  
      
      
            End If  
      
            If selectedItem3 = lbl30.Text Then  
                lbl30.BackColor = Color.DarkGreen  
                MsgBox("Winner of " & FormatCurrency(SingleWin.ToString))  
      
      
            End If  
      
            If selectedItem4 = lbl40.Text Then  
                lbl40.BackColor = Color.DarkGreen  
                MsgBox("Winner of " & FormatCurrency(SingleWin.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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    It is more stream lined if you use control arrays. The following is in VB6, but you get the idea. There are four labels and four combo boxes configured as control arrays. The combo box lists are preloaded with strings "One" to "Ten", and the lblWin box is made visible and flashes at different rates upon a win.
    Code:
    Private Sub cmdPlay_Click()
        Dim WinCount As Integer
        Dim N%
        Const SingleWin As String = "$100.00"
        Const DoubleWin As String = "$300.00"
        Const TripleWin As String = "$500.00"
        Const JackPot As String = "$1000.00"
        Randomize
        Timer1.Enabled = 0
        lblWin.Visible = False
        For N% = 0 To 3
            Label1(N%).BackColor = &H8000000F
            Label1(N%) = Combo1(N%).List(Int(10 * Rnd))
        Next N%
        For N% = 0 To 3
            If Combo1(N%).Text = Label1(N%).Caption Then
                Label1(N%).BackColor = &HFF00&     'Color.DarkGreen
                WinCount = WinCount + 1
            End If
        Next N%
        Select Case WinCount
            Case 0
                lblWin.Visible = False
            Case 1
                lblWin.Caption = SingleWin
                lblWin.Visible = True
                Timer1.Interval = 1000
                Timer1.Enabled = True
            Case 2
                lblWin.Caption = DoubleWin
                lblWin.Visible = True
                Timer1.Interval = 500
                Timer1.Enabled = True
            Case 3
                lblWin.Caption = TripleWin
                lblWin.Visible = True
                Timer1.Interval = 250
                Timer1.Enabled = True
            Case 4
                lblWin.Caption = JackPot
                lblWin.Visible = True
                Timer1.Interval = 100
                Timer1.Enabled = True
        End Select
    End Sub
    
    Private Sub Timer1_Timer()
        lblWin.Visible = Not lblWin.Visible
    End Sub
    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0
    Thanks for this info! This is valuable information for me on using arrays which I'm currently unfamiliar with.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    test for wins


    e.g

    number_of_wins = 0

    for xxx= 1 to number_0f_chances
    'code which works out if this is a win
    if this_is_ a_ win then
    number_of_wins = number_of_wins + 1
    end if
    next xxx

    if number_of_wins = 4 then
    msgbox "You've hit the jackpot"
    end if

    (Can also use Case statement - but that is not always more elegant)

    'etc etc

    if number_of_wins = 0 then
    msgbox "Hey man you suck!"
    end if
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0
    Yes, this fits the pattern of what my instructor showed me the solution was the other day. I turned in this assignment awhile ago. Fortunately, it was only worth ten points towards my grade.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0
    Thanks for the info guys.... i was also looking for the same!!...thx!

IMN logo majestic logo threadwatch logo seochat tools logo