Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Working from scratch based on created application

Working from scratch based on created application

Scheduled Pinned Locked Moved Visual Basic
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Herboren
    wrote on last edited by
    #1

    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
    
    L D 2 Replies Last reply
    0
    • H Herboren

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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[^]

      1 Reply Last reply
      0
      • H Herboren

        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
        
        D Offline
        D Offline
        Dominick Marciano
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups