1 Replies - 610 Views - Last Post: 04 April 2008 - 10:02 PM Rate Topic: -----

#1 sherrill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-March 08

program works...is there better code?

Post icon  Posted 04 April 2008 - 09:38 PM

this random number generator works however i'm wondering if the code for checking numbers that match can be done a different way?...thanks if someone would respond.....i use an entry form and entry module its an instructor request so that's how it had to be done



    Const intMAX_VALUES As Integer = 4          ' Array upper subscript
    Dim intDigits(intMAX_VALUES) As Integer     ' Loop counter
    Private rand As New Random(9)               ' Random generator 
    Dim intNumber(4) As Integer                 ' Holds random numbers

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

        ' Enter the five numbers into list box.
        Dim intCount As Integer

        ' Get input and display in list box.
        For intCount = 0 To intMAX_VALUES
            intDigits(intCount) = GetIntInput()
        Next

        ' Display input value header.
        lstInput.Items.Add("Lottery Numbers")
        lstInput.Items.Add("________________")
        lstInput.Items.Add(" ")

        ' Display the input values.
        For intCount = 0 To intMAX_VALUES
            lstInput.Items.Add(intDigits(intCount))
        Next

        ' Disables input button after entering user numbers.
        btnInput.Enabled = False

    End Sub

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        
        ' Generates random numbers
        intNumber(0) = rand.Next(9)
        intNumber(1) = rand.Next(9)
        intNumber(2) = rand.Next(9)
        intNumber(3) = rand.Next(9)
        intNumber(4) = rand.Next(9)

        ' Enters random numbers into labels.
        lblNum1.Text = CStr(intNumber(0))
        lblNum2.Text = CStr(intNumber(1))
        lblNum3.Text = CStr(intNumber(2))
        lblNum4.Text = CStr(intNumber(3))
        lblNum5.Text = CStr(intNumber(4))

        ' Disables random number generator after entering numbers.
        btnGenerate.Enabled = False

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        ' Clears the list box and labels.
        lstInput.Items.Clear()
        lblNum1.Text = ""
        lblNum2.Text = ""
        lblNum3.Text = ""
        lblNum4.Text = ""
        lblNum5.Text = ""

        ' Enables input button and random number buttons.
        btnInput.Enabled = True
        btnGenerate.Enabled = True

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        ' Ends the application.
        Me.Close()

    End Sub

    Private Sub btnMatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMatch.Click

        ' Calculate the numbers that match.
        Dim intCount As Integer
        Dim intWin As Integer = 0               ' All digits match
        Dim intMatch1 As Boolean = False
        Dim intMatch2 As Boolean = False
        Dim intMatch3 As Boolean = False
        Dim intMatch4 As Boolean = False
        Dim intMatch5 As Boolean = False
        Dim intMatch6 As Integer = -1

        ' Looks for matching numbers
        For intCount = 0 To intMAX_VALUES
            If intDigits(0) = intNumber(intCount) And intMatch1 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch1 = True
                intMatch6 = intCount
            End If
            If intDigits(1) = intNumber(intCount) And intMatch2 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch2 = True
                intMatch6 = intCount
            End If
            If intDigits(2) = intNumber(intCount) And intMatch3 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch3 = True
                intMatch6 = intCount
            End If
            If intDigits(3) = intNumber(intCount) And intMatch4 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch4 = True
                intMatch6 = intCount

            End If
            If intDigits(4) = intNumber(intCount) And intMatch5 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch5 = True
                intMatch6 = intCount
            End If
        Next

        MessageBox.Show(CStr(intWin))

        ' Message box proclaiming grand prize winner
        If intWin = 5 Then
            MessageBox.Show("Grand Prize Winner", "Congradulations")
        End If

    End Sub
End Class

entry form code

 Dim intDigits As Integer = 0    ' Class level variable
    Dim strDigits As String         ' Holds the length of integer

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        'check input textbox for numeric, if it is store in class level variable
        If EntriesValid(txtDigits) Then
            intDigits = CInt(txtDigits.Text)
            Me.Close()
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Function EntriesValid(ByVal aTxtObject As TextBox) As Boolean

        Dim InputsOK As Boolean = True
        strDigits = txtDigits.Text

        If IsNumeric(aTxtObject.Text) Then
        End If
        If strDigits.Length <= 1 Then
            errError.SetError(aTxtObject, "")
        Else
            errError.SetError(aTxtObject, "The input is numeric, 1 integer in length, and  between 0 and 9")
            InputsOK = False
        End If
        Return InputsOK

    End Function

    Public Function GetInput() As Integer
        'function to return class level variable, note variable is not global
        Return intDigits
    End Function
End Class

entry module code
Module EntryModule

    Public Function GetIntInput() As Integer

        'GetIntInput function to create an instance of entry form
        'ShowDialog the form and wait for form to close
        'The GetInput function will return the input value and pass to calling function

        Dim entryForm As New frmEntry
        entryForm.ShowDialog()
        Return entryForm.GetInput()
    End Function

End Module




EDIT: Please use code tags => :code:

This post has been edited by PsychoCoder: 04 April 2008 - 09:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: program works...is there better code?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: program works...is there better code?

Posted 04 April 2008 - 10:02 PM

Well there are many things you can do with this... here is a great example of cleaning it up...

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        
        ' Generates random numbers
        intNumber(0) = rand.Next(9)
        intNumber(1) = rand.Next(9)
        intNumber(2) = rand.Next(9)
        intNumber(3) = rand.Next(9)
        intNumber(4) = rand.Next(9)

        ' Enters random numbers into labels.
        lblNum1.Text = CStr(intNumber(0))
        lblNum2.Text = CStr(intNumber(1))
        lblNum3.Text = CStr(intNumber(2))
        lblNum4.Text = CStr(intNumber(3))
        lblNum5.Text = CStr(intNumber(4))

        ' Disables random number generator after entering numbers.
        btnGenerate.Enabled = False

    End Sub



First of all when you start seeing repeat statements like rand.Next(9) over and over, that is telling you that you could use a loop. In this case since it is an array you could even do a loop going from 0 to the array size -1.

Code like the bottom here...

If intDigits(0) = intNumber(intCount) And intMatch1 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch1 = True
                intMatch6 = intCount
            End If
            If intDigits(1) = intNumber(intCount) And intMatch2 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch2 = True
                intMatch6 = intCount
            End If
            If intDigits(2) = intNumber(intCount) And intMatch3 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch3 = True
                intMatch6 = intCount
            End If
            If intDigits(3) = intNumber(intCount) And intMatch4 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch4 = True
                intMatch6 = intCount

            End If
            If intDigits(4) = intNumber(intCount) And intMatch5 = False And intMatch6 <> intCount Then
                intWin = intWin + 1
                intMatch5 = True
                intMatch6 = intCount
            End If



See how each if statement is almost identical except for a few numbers like intdigits(4) and intMatch5 etc? This is asking for you to "refactor" the code into its own function where you pass in the number 4 and intMatch5 as parameters. That way you have only one copy of the code and you cut this code from 25 lines down to about 10. Not to mention increases reusability of code because then you could call the function later. It also makes it easier to maintain because you just need to change the logic in one place.

In the EntriesValid routine you have this code...

  
If IsNumeric(aTxtObject.Text) Then  
End If 



This code is above is doing nothing. You might as well just take it out.

This is just some of the changes, many more can be done to increase efficiency and cut down on code. Just take a few minutes to figure out what you could do differently to make this code work more with less.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1