I can not seem to get my amounts to add up

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

62 Replies - 3928 Views - Last Post: 25 February 2016 - 12:10 PM Rate Topic: -----

#1 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

I can not seem to get my amounts to add up

Posted 13 February 2016 - 02:18 AM

going crazy

I can not seem to get my amounts to add up. I have tried everything I can think of and still it doesnt work. I tried to type it out exactly as the book say but it just errors out. Most of the errors I get say that with option strict on you can not convert. I know I have something wrong in the code but I just cant figure it out. I have been over the book and emailed my instructer numerous times with no assistance. He does answer once a week but its kryptic and not understandable. Also my values for totaltextbox shows up as $0.00 all the time as does the tax box. Any ideas? Below is what I have. Please email me if you need information quickly.

  'Declairs module level constants.
    Const delivery_decimal As Decimal = 2D
    Const tax_rate_decimal As Decimal = 0.825D
    Const small_price_decimal As Decimal = 6D
    Const medium_price_decimal As Decimal = 10D
    Const large_price_decimal As Decimal = 15D
    Const small_top_decimal As Decimal = 1D
    Const medium_top_decimal As Decimal = 1.5D
    Const large_top_decimal As Decimal = 2D
    Dim total_decimal As Decimal
    Dim subtotal_decimal As Decimal


    Private Sub exitcmd_Click(sender As System.Object, e As System.EventArgs) Handles exitcmd.Click
        'exits the program
        Close()

    End Sub

    Private Sub newordercmd_Click(sender As System.Object, e As System.EventArgs) Handles newordercmd.Click
        'clears the form of all data
        subtotaltextbox.Text = ""
        taxtextbox.Text = ""
        totaltextbox.Text = ""
        costtextbox.Text = ""
        addresstextbox.Text = ""
        deliverytextbox.Text = ""
        With nametextbox
            .Clear()
            .Focus()
        End With

        'defaults pizza size to small
        smallrb.Checked = True

        'defaults toppings to cheese
        cheesecb.Checked = True
        pepperonicb.Checked = False
        sausagecb.Checked = False
        mushroomscb.Checked = False

        'sets for pickup
        pickuprb.Checked = True



    End Sub

    Private Sub deliveryrb_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles deliveryrb.CheckedChanged
        'shows/hides the address box and delivery charges if needed
        Dim delivery_decimal = FormatCurrency(2.0)

        If deliveryrb.Checked = True Then
            deliverytextbox.Text = delivery_decimal.ToString
        End If

        If deliveryrb.Checked = True Then
            addresstextbox.Visible = True
        ElseIf pickuprb.Checked = True Then
            addresstextbox.Visible = False
        End If

        If pickuprb.Checked = True Then
            deliverytextbox.Text = ""
        End If

    End Sub

    Private Sub calculatecmd_Click(sender As System.Object, e As System.EventArgs) Handles calculatecmd.Click
        'adds up all the charges based on the selections and displays the total

        Dim small_price_decimal = FormatCurrency(6D)
        Dim medium_price_decimal = FormatCurrency(10.0)
        Dim large_price_decimal = FormatCurrency(15.0)
        Dim smallonetop_decimal = FormatCurrency(1.0)
        Dim mediumonetop_decimal = FormatCurrency(1.5)
        Dim largeonetop_decimal = FormatCurrency(2.0)
        Dim subtotal_decimal, delivery_decimal, total_decimal, taxdecimal As Decimal

        Try
            If nametextbox.Text = "" Then
                MessageBox.Show("Please enter the customers name")
                nametextbox.Focus()
            End If
        Catch ex As Exception
        End Try

        'displays the total


        If smallrb.Checked = True Then
            subtotaltextbox.Text = small_price_decimal.ToString

        ElseIf mediumrb.Checked = True Then
            subtotaltextbox.Text = medium_price_decimal.ToString
        ElseIf largerb.Checked = True Then
            subtotaltextbox.Text = large_price_decimal.ToString
        End If

        If smallrb.Checked = True Then
            costtextbox.Text = smallonetop_decimal
        ElseIf mediumrb.Checked = True Then
            costtextbox.Text = mediumonetop_decimal
        ElseIf largerb.Checked = True Then
            costtextbox.Text = largeonetop_decimal
        End If



        taxdecimal = subtotal_decimal * tax_rate_decimal
        taxtextbox.Text = taxdecimal.ToString("C")

        total_decimal = tax_rate_decimal * subtotal_decimal + delivery_decimal
        totaltextbox.Text = total_decimal.ToString("C")


    End Sub


Btw this is being done in Visual Basic 10

This post has been edited by andrewsw: 13 February 2016 - 02:32 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: I can not seem to get my amounts to add up

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: I can not seem to get my amounts to add up

Posted 13 February 2016 - 02:35 AM

This is VB.NET code, not VB6. Question moved to VB.NET forum.

How to use code tags:

Attached Image

Please use a meaningful title for your question, not "going crazy".

What is the first error - the exact details - and what line in your posted code does it refer to?
Was This Post Helpful? 0
  • +
  • -

#3 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 475
  • View blog
  • Posts: 1,440
  • Joined: 02-November 11

Re: I can not seem to get my amounts to add up

Posted 15 February 2016 - 06:00 AM

Quote

Most of the errors I get say that with option strict on you can not convert.


This here is fairly rudimentary but I hit the same hurdle when I was just starting too.

Right Now I am looking just at one method as I think this is where your error is coming from, but it would help me more than you could imagine if you could post the actual error and indicate what line it is on.

The Problem is calculation, so lets look at your calculation sub:

Private Sub calculatecmd_Click(sender As System.Object, e As System.EventArgs) Handles calculatecmd.Click
      'adds up all the charges based on the selections and displays the total

      Dim small_price_decimal = FormatCurrency(6D)
      Dim medium_price_decimal = FormatCurrency(10.0)
      Dim large_price_decimal = FormatCurrency(15.0)
      Dim smallonetop_decimal = FormatCurrency(1.0)
      Dim mediumonetop_decimal = FormatCurrency(1.5)
      Dim largeonetop_decimal = FormatCurrency(2.0)
      Dim subtotal_decimal, delivery_decimal, total_decimal, taxdecimal As Decimal

      Try
          If nametextbox.Text = "" Then
              MessageBox.Show("Please enter the customers name")
              nametextbox.Focus()
          End If
      Catch ex As Exception
      End Try

      'displays the total


      If smallrb.Checked = True Then
          subtotaltextbox.Text = small_price_decimal.ToString

      ElseIf mediumrb.Checked = True Then
          subtotaltextbox.Text = medium_price_decimal.ToString
      ElseIf largerb.Checked = True Then
          subtotaltextbox.Text = large_price_decimal.ToString
      End If

      If smallrb.Checked = True Then
          costtextbox.Text = smallonetop_decimal
      ElseIf mediumrb.Checked = True Then
          costtextbox.Text = mediumonetop_decimal
      ElseIf largerb.Checked = True Then
          costtextbox.Text = largeonetop_decimal
      End If



      taxdecimal = subtotal_decimal * tax_rate_decimal
      taxtextbox.Text = taxdecimal.ToString("C")

      total_decimal = tax_rate_decimal * subtotal_decimal + delivery_decimal
      totaltextbox.Text = total_decimal.ToString("C")
End Sub


First off, this can be condensed as both if statements are the same (and adding .ToString() to the ones that are missing it should solve your Option Strict Problem ;)):

If smallrb.Checked = True Then
          subtotaltextbox.Text = small_price_decimal.ToString

      ElseIf mediumrb.Checked = True Then
          subtotaltextbox.Text = medium_price_decimal.ToString
      ElseIf largerb.Checked = True Then
          subtotaltextbox.Text = large_price_decimal.ToString
      End If

      If smallrb.Checked = True Then
          costtextbox.Text = smallonetop_decimal
      ElseIf mediumrb.Checked = True Then
          costtextbox.Text = mediumonetop_decimal
      ElseIf largerb.Checked = True Then
          costtextbox.Text = largeonetop_decimal
      End If


Becomes:

If smallrb.Checked = True Then
          subtotaltextbox.Text = small_price_decimal.ToString()
          costtextbox.Text = smallonetop_decimal.ToString()
      ElseIf mediumrb.Checked = True Then
          subtotaltextbox.Text = medium_price_decimal.ToString()
          costtextbox.Text = mediumonetop_decimal.ToString()
      ElseIf largerb.Checked = True Then
          subtotaltextbox.Text = large_price_decimal.ToString()
          costtextbox.Text = largeonetop_decimal.ToString()
      End If


Then Lets look at the Calculations themselves:

Values as Defined at point of calculations:
subtotal_decimal = 0D
delivery_decimal = 0D
total_decimal = 0D
taxdecimal = 0D
tax_rate_decimal = 0.825D


Which Makes The Equations:
taxdecimal = subtotal_decimal * tax_rate_decimal
'taxdecimal = 0 * 0.825 = 0

total_decimal = tax_rate_decimal * subtotal_decimal + delivery_decimal
'total_decimal = 0.825 * 0 + 0 = 0


So there you go, that is why your values are coming back as 0... When in doubt, substitute. In reality this whole thing is just algebra :)
Was This Post Helpful? 0
  • +
  • -

#4 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

Re: I can not seem to get my amounts to add up

Posted 15 February 2016 - 07:05 PM

I changed those things you noted and I have the same results. So, I am still not getting my total no and I am not getting my tax amount to display. Nothing comes up in those boxes at all.

 Private Sub calculatecmd_Click(sender As System.Object, e As System.EventArgs) Handles calculatecmd.Click
        'adds up all the charges based on the selections and displays the total

        Dim small_price As Decimal = 6D
        Dim medium_price As Decimal = 10D
        Dim large_price As Decimal = 15D
        Dim smallonetop As Decimal = 1D
        Dim mediumonetop As Decimal = 1.5D
        Dim largeonetop As Decimal = 2D
        Dim delivery = 2D
        Dim taxrate = 0.825D


        Try
            If nametextbox.Text = "" Then
                MessageBox.Show("Please enter the customers name")
                nametextbox.Focus()
            End If
        Catch ex As Exception
        End Try

        'displays the total

        If smallrb.Checked = True Then
            subtotaltextbox.Text = small_price.ToString("C")
            costtextbox.Text = smallonetop.ToString("C")

        ElseIf mediumrb.Checked = True Then
            subtotaltextbox.Text = medium_price.ToString("C")
            costtextbox.Text = mediumonetop.ToString("C")

        ElseIf largerb.Checked = True Then
            subtotaltextbox.Text = large_price.ToString("C")
            costtextbox.Text = largeonetop.ToString("C")
        End If

        'tax =0.825=0
        taxrate = subtotal * taxrate

        'total=0.825*0+0=0
        total = tax * subtotal + delivery
        
       
    End Sub

This post has been edited by IronRazer: 16 February 2016 - 12:06 PM
Reason for edit:: Added Code Tags

Was This Post Helpful? 0
  • +
  • -

#5 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 475
  • View blog
  • Posts: 1,440
  • Joined: 02-November 11

Re: I can not seem to get my amounts to add up

Posted 15 February 2016 - 07:28 PM

Put your code in code tags for me and I'll look at this when I get to a computer in the morning. My phone screen is too small to lay it all out properly.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 01:17 AM

Revisit my screencast in post #2.
Was This Post Helpful? 0
  • +
  • -

#7 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 475
  • View blog
  • Posts: 1,440
  • Joined: 02-November 11

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 04:52 AM

I'll bet you $100 that the subtotal variable is still 0.

Aside from that, the following assigns the values to a variable, not to a TextBox.Text property
taxrate = subtotal * taxrate

total = tax * subtotal + delivery

Was This Post Helpful? 0
  • +
  • -

#8 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 09:23 AM

I dont know what you mean by code snippet. Second how is my subtotal variable still 0?.Its not supposed to be at all. As i have said I can not make since of what I have going on here.


    Private Sub calculatecmd_Click(sender As System.Object, e As System.EventArgs) Handles calculatecmd.Click
        'adds up all the charges based on the selections and displays the total

        Dim small_price As Decimal = 6D
        Dim medium_price As Decimal = 10D
        Dim large_price As Decimal = 15D
        Dim smallonetop As Decimal = 1D
        Dim mediumonetop As Decimal = 1.5D
        Dim largeonetop As Decimal = 2D
        Dim delivery = 2D
        Dim taxrate = 0.825D


        Try
            If nametextbox.Text = "" Then
                MessageBox.Show("Please enter the customers name")
                nametextbox.Focus()
            End If
        Catch ex As Exception
        End Try

        'displays the total

        If smallrb.Checked = True Then
            subtotaltextbox.Text = small_price.ToString("C")
            costtextbox.Text = smallonetop.ToString("C")

        ElseIf mediumrb.Checked = True Then
            subtotaltextbox.Text = medium_price.ToString("C")
            costtextbox.Text = mediumonetop.ToString("C")

        ElseIf largerb.Checked = True Then
            subtotaltextbox.Text = large_price.ToString("C")
            costtextbox.Text = largeonetop.ToString("C")
        End If

        'tax =0.825=0
        taxrate = subtotal * taxrate

        'total=0.825*0+0=0
        total = tax * subtotal + delivery

    End Sub
:code:

This post has been edited by modi123_1: 16 February 2016 - 09:31 AM
Reason for edit:: Please add 'code tags' to your post by highlighting the text that is your code and click the 'code' button in the editor.

Was This Post Helpful? 0
  • +
  • -

#9 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 09:33 AM

so then how do i change the subtotal variable to a value from 0? It has to be able to be changed as well to another $value depending on what if statements are true. Ive changed it over and over and can not see to find the right code to get it to work.
Was This Post Helpful? 0
  • +
  • -

#10 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 475
  • View blog
  • Posts: 1,440
  • Joined: 02-November 11

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 09:40 AM

your subtotal is still 0 because you are not setting it at all.

I'm going to assume this is where you think you are setting it:

If smallrb.Checked = True Then
        subtotaltextbox.Text = small_price.ToString("C")
        costtextbox.Text = smallonetop.ToString("C")

    ElseIf mediumrb.Checked = True Then
        subtotaltextbox.Text = medium_price.ToString("C")
        costtextbox.Text = mediumonetop.ToString("C")

    ElseIf largerb.Checked = True Then
        subtotaltextbox.Text = large_price.ToString("C")
        costtextbox.Text = largeonetop.ToString("C")
    End If


The subtotal VARIABLE is not present here, just a textbox.
Was This Post Helpful? 0
  • +
  • -

#11 PeterH   User is offline

  • D.I.C Regular

Reputation: 60
  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 09:55 AM

As TechnoBear has said, you seem to be setting the subtotaltextbox.text to a price value, but you are using the subtotal variable to perform the calculation, which is never set to anything, I assume you should be doing something like,
 subtotal = cdec(subtotaltextbox.text)



before doing the calculation step.
Was This Post Helpful? 0
  • +
  • -

#12 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,218
  • Joined: 12-January 10

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 11:56 AM

if you want to show the subtotal you would need something like:


someTextbox.text = subtotal.toString()

Was This Post Helpful? 0
  • +
  • -

#13 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 12:09 PM

Peter H.
That code causes the program to stop and error out. This is the problem I have been having for 3 days!
Was This Post Helpful? 0
  • +
  • -

#14 ewoods8393   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 13-February 16

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 12:14 PM

I have tried everything I know to try and nothing seems to make it work the way it supposed to. Try doing a toparse for the subtotaltextbox and still nothing. I have re written the entire calculate code 4 times with no success at all. This option strict on thing has really put me in a spot.
Was This Post Helpful? 0
  • +
  • -

#15 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 475
  • View blog
  • Posts: 1,440
  • Joined: 02-November 11

Re: I can not seem to get my amounts to add up

Posted 16 February 2016 - 12:22 PM

Private Sub calculatecmd_Click(sender As System.Object, e As System.EventArgs) Handles calculatecmd.Click
    'adds up all the charges based on the selections and displays the total
    Dim small_price As Decimal = 6D
    Dim medium_price As Decimal = 10D
    Dim large_price As Decimal = 15D
    Dim smallonetop As Decimal = 1D
    Dim mediumonetop As Decimal = 1.5D
    Dim largeonetop As Decimal = 2D
    Dim delivery = 2D
    Dim taxrate = 0.825D


    Try
        If nametextbox.Text = "" Then
            MessageBox.Show("Please enter the customers name")
            nametextbox.Focus()
        End If
    Catch ex As Exception
    End Try

    'displays the total

    If smallrb.Checked = True Then
        subtotal = small_price.ToString("C")
        costtextbox.Text = smallonetop.ToString("C")

    ElseIf mediumrb.Checked = True Then
        subtotal = medium_price.ToString("C")
        costtextbox.Text = mediumonetop.ToString("C")

    ElseIf largerb.Checked = True Then
        subtotal = large_price.ToString("C")
        costtextbox.Text = largeonetop.ToString("C")
    End If

    subtotaltextbox.Text = subtotal

    taxrate = subtotal * taxrate
    taxtextbox.Text = taxrate.ToString()

    total = tax * subtotal + delivery
    totaltextbox.Text = total.ToString()
End Sub


I got bored, pretty sure that'll fix it
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »