Click here to Skip to main content
       

Visual Basic

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWorking from scratch based on created applicationmemberHerboren3-Oct-12 5:49 
I have a teacher who gave us a compiled application. He did not provide us the code. We have to generate the code in our head and write it out. It must act like his application in every possible way. I have it written out but for some reason when I type in the value of two(2) pieces the two(2) isn't calculated yet every other number I type calculates just fine. Why?
 
two(2) falls within range of 1-199 so its price would be calculated as rangeA * two(2) pieces = price for two, but no result is returned, my text does not display in the textbox either.
 
 Dim priceArrayElements(20) As Decimal ' Store Prices calculated, 21 count
    Dim pieceArrayElements(20) As Integer ' Store Pieces counted, 21 count

    Dim addedPieces ' Total of all Piece elements added together
    Dim totalAveragePieces ' Average of Piece elements added together / element count

    Dim addedPrices As Decimal ' Total of all price elements added together
    Dim totalAveragePrices As Decimal ' Average of Price elements added together / element count

    Dim elementCounter As Decimal ' Global Counter for both arrays

    Dim rangeA As Decimal = 0.5  ' Price applies to range   1 - 199
    Dim rangeB As Decimal = 0.55 ' Price applies to range 200 - 399
    Dim rangeC As Decimal = 0.6  ' Price applies to range 400 - 599
    Dim rangeD As Decimal = 0.65 ' Price applies to range 600 - >

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case False
            Case IsNumeric(txtPieces.Text)
                MessageBox.Show("Pieces completed must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtPieces.Text = ""
 
            Case Not IsNumeric(txtName.Text)
                MessageBox.Show("Name is required.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtName.Text = ""
 
            Case Else
 
                Select Case txtPieces.Text
                    Case 1 To 199
                        Try
                            For i As Integer = 0 To priceArrayElements.Length
                                priceArrayElements(i) = (Decimal.Parse(txtPieces.Text) * rangeA)
                                txtEarned.Text = FormatCurrency(priceArrayElements(i).ToString())
                            Next
                        Catch ex As Exception
                            'Supress Message
                        End Try
                    Case 200 To 399
                        Try
                            For i As Integer = 0 To priceArrayElements.Length
                                priceArrayElements(i) = (Decimal.Parse(txtPieces.Text) * rangeB)
                                txtEarned.Text = FormatCurrency(priceArrayElements(i).ToString())
                            Next
                        Catch ex As Exception
                            'Supress Message
                        End Try
                    Case 400 To 599
                        Try
                            For i As Integer = 0 To priceArrayElements.Length
                                priceArrayElements(i) = (Decimal.Parse(txtPieces.Text) * rangeC)
                                txtEarned.Text = FormatCurrency(priceArrayElements(i).ToString())
                            Next
                        Catch ex As Exception
                            'Supress Message
                        End Try
                    Case Else
                        If Int32.Parse(txtPieces.Text) >= 600 Then
                            Try
                                For i As Integer = 0 To priceArrayElements.Length
                                    priceArrayElements(i) = (Decimal.Parse(txtPieces.Text) * rangeD)
                                    txtEarned.Text = FormatCurrency(priceArrayElements(i).ToString())
                                Next
                            Catch ex As Exception
                                'Supress Message
                            End Try
                        End If
                End Select
 
        End Select
 
    End Sub
 
End Class

AnswerRe: Working from scratch based on created application PinmemberEddy Vluggen3-Oct-12 22:46 
Download ILSpy, and check out the code that your teacher wrote Smile | :)
 
As opposed to suppressing messages, you might want to log them to a file. If the '2' goes wrong due to an exception, you'd at least get a good message telling you what went wrong. Throwing away error-information is always a bad idea.
Bastard Programmer from Hell Suspicious | :suss:
if you can't read my code, try converting it here[^]

AnswerRe: Working from scratch based on created application PinmemberDisIsHoody11-Oct-12 19:02 
Quick thing I noticed:
 
Herboren wrote:
Dim priceArrayElements(20) As Decimal

and
Herboren wrote:
For i As Integer = 0 To priceArrayElements.Length
priceArrayElements(i) = (Decimal.Parse(txtPieces.Text) * rangeA)
txtEarned.Text = FormatCurrency(priceArrayElements(i).ToString())
Next

The length of priceArrayElements would be 21 (the total number of elements), however the highest index is 20. So in the for loop the last index it would try to use would be 21, however the highest index would be 20. Shouldn't the for loop be:
 
For i As Integer = 0 To priceArrayElements.Length - 1
'Do calculations
Next
 
or
 
For i As Integer = 0 To priceArrayElements.GetUpperBound(0)
'Do calculations
Next

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130619.1 | Last Updated 19 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid