Working from scratch based on created application
-
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 countDim 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
-
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 countDim 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
Download ILSpy, and check out the code that your teacher wrote :) 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 :suss: if you can't read my code, try converting it here[^]
-
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 countDim 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
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
Nextor
For i As Integer = 0 To priceArrayElements.GetUpperBound(0)
'Do calculations
Next