• 0

VB - Retail calculator


Question

I am having some major issues. The assignment

 

write an application to determine a markup cost. must contain a function called CalculateRetail

when clicking a "get retail" button, (btnCalc) program should..

verify values are positive

call the calculateretail function

display retail cost as displayed by function

 

basically, if wholesale is $5, markup is 100%, it should show price as $10

 

so there are 3 boxes.

 

textbox 1 = txtWholesale

textbox 2 = txtMarkup

Label 1 = lblRetail 

 

must use option strict

TRY / CATCH cannot be used.

must show if input is numeric

must have a "clear" button to clear all boxes

 

 

here is my code which i am sure has all sorts of issues as i am apparently not very good at VB... decent enough with HTML but for some reason, VB is hard for me.

Option Strict On
Public Class Form1


    Private decWholesale As Decimal 'to hold wholesale price
    Private decMarkupPrice As Decimal    'to hold markup %
    Private DecMarkup As Decimal


    Private Function Validatefields() As String
        'Give message if not numeric and under 1 as a whole number
        If IsNumeric(txtWholesale.Text) Then
            lblWarning.Text = "Wholesale Price must be numeric and whole number"
            Return CStr(False)
        End If

        If IsNumeric(txtMarkup.Text) Then
            lblWarning.Text = "Markup amount must be numeric and whole number"
            Return CStr(False)


        End If
        Return CStr(True)

    End Function

    Function CalculateRetail(ByVal decretail As Decimal, ByVal decMarkup As Decimal) As Decimal
        'calculate and return markup price
        Dim decMarkupPrice As Decimal
        Dim decPercentage As Decimal

        decPercentage = CDec(decMarkup * 0.01)
        decMarkupPrice = decWholesale + (decWholesale * decPercentage)
        Return decMarkupPrice
    End Function

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

        Dim decMarkupPrice As Decimal

        'clear previous warning
        lblWarning.Text = String.Empty
        txtWholesale.Text = String.Empty
        txtMarkup.Text = String.Empty
        'input fields if valid
        If Validatefields() Then
            decMarkupPrice = CalculateRetail(decWholesale, decMarkup)
            lblRetail.Text = decMarkupPrice.ToString("c")
        End If

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'clear previous messages
        lblWarning.Text = String.Empty
        txtWholesale.Text = String.Empty
        txtMarkup.Text = String.Empty



    End Sub

End Class

if you need any other info or anything, just let me know. i am just lost right now. 

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

so after looking at this for a bit, i still have yet to figure out how to make it work correctly.

 

The issues i see are that the input data is not properly being retrieved by the function and then thrown over to the program itself, and then displayed correctly. I believe i am having some sort of issue there..... for starters. any suggestions on where i should look would be greatly appreciated. Also, any specific resources you could point me to that pertains to the issues in my poor coding would be very helpful as well. Again, i really do kinda suck at VB and i am just hanging on by a thread. 

Link to comment
Share on other sites

  • 0

Have you tried stepping through the code in the debugger?

 

I think I can see the issue, but I am not going to give you the answer.  You can find it yourself if your approach to finding it is right, and that is what you need to learn - we'll be happy to help you on that journey...

 

From what I can see you have made a relatively silly error (the sort you wonder why you didn't spot once you know what it is) on a single line of this code. The rest looks alright.

Link to comment
Share on other sites

  • 0

decMarkupPrice = decRetail + (decRetail * decPercentage)

Also, why did you do validatefields as string and not Boolean?

Moreover, you still need to check if the numbers entered are positive

Edit: helped since this bothered you for two days

i did it as string and not boolean because i am a fool. like i mentioned, i am not that great. I am currently at the point of trying to find references elsewhere in my book and modify them to work as needed.

 

as for checking if positive....

If IsNumeric(txtWholesale.Text) Then
            lblWarning.Text = "Wholesale Price must be numeric and whole number"
            Return CStr(False)
        End If

this is what i have. i will check when i get home and i have my programming with me, but it should say to return true opposed to false in this case because it is checking if it is numeric, and since i want it to send a true if it is numeric correct?

Link to comment
Share on other sites

  • 0

Have you tried stepping through the code in the debugger?

 

I think I can see the issue, but I am not going to give you the answer.  You can find it yourself if your approach to finding it is right, and that is what you need to learn - we'll be happy to help you on that journey...

 

From what I can see you have made a relatively silly error (the sort you wonder why you didn't spot once you know what it is) on a single line of this code. The rest looks alright.

as the guy under showed me it REALLY is one of those "wtf" type errors. as for stepping through the degugging, is there a way to edit while debug is running? 

 

i tried once and it said i had to turn something on or off and i got all confused. 

Link to comment
Share on other sites

  • 0

so i checked again and it is still returning a 0.00 value. like i did $5 wholesale and 100% markup as the example gives, should display $10. At this rate I am going to be very late with the assignment but I dont care. I want to get it done. I would rather turn in the assignment late and working than on time and not working. I am determined..... but even if i am determined, my understanding is lacking and i just cant figure out why....

 

so this is what i am up to. full grade or any grade be damned i will finish this.

 

so.... when i try and do a check to make sure the boxes are > 1 it says it can not use string as double with option strict on. i am at a loss as to how to fix this. any references i can check out that can help me with this that you all know of online?

 

i have tried running as debug yet i do not get any errors, yet it doesnt work so it does not appear that there are any major syntax errors, opposed to the ones mentioned in the check if  > 1 statement

Option Strict On
Public Class Form1


    Private decWholesale As Decimal 'to hold wholesale price
    Private decMarkupPrice As Decimal    'to hold markup %
    Private DecMarkup As Decimal


    Private Function Validatefields() As Boolean
        'is numeric check
        If Not IsNumeric(txtWholesale.Text) Then
            lblWarning.Text = "Wholesale Price must be numeric and whole number"
            Return CBool(False)
        End If

        If Not IsNumeric(txtMarkup.Text) Then
            lblWarning.Text = "Markup amount must be numeric and whole number"
            Return CBool(False)
        End If

        'is wrong
        'Greater than 0 check
        If (txtMarkup.Text) < 1 Then
            lblWarning.Text = "Markup amount must be numeric and whole number"
            Return CBool(False)
        End If

        If (txtWholesale.Text) < 1 Then
            lblWarning.Text = "Wholesale Price must be numeric and whole number"
            Return CBool(False)

        End If
        Return CBool(True)

    End Function

    Function CalculateRetail(ByVal decretail As Decimal, ByVal decMarkup As Decimal) As Decimal
        'calculate and return markup price
        Dim decMarkupPrice As Decimal
        Dim decPercentage As Decimal

        decPercentage = CDec(decMarkup * 0.01)
        decMarkupPrice = decWholesale + (decretail * decPercentage)
        Return decMarkupPrice
    End Function

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click



        'input fields if valid
        If Validatefields() Then
            decMarkupPrice = CalculateRetail(decWholesale, decMarkup)
            lblRetail.Text = decMarkupPrice.ToString("c")
        End If

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'clear previous messages
        lblWarning.Text = String.Empty
        txtWholesale.Text = String.Empty
        txtMarkup.Text = String.Empty



    End Sub

End Class

Link to comment
Share on other sites

  • 0

as the guy under showed me it REALLY is one of those "wtf" type errors. as for stepping through the degugging, is there a way to edit while debug is running? 

 

i tried once and it said i had to turn something on or off and i got all confused. 

He is right. You can't change your code (yet) while debugging, as you're literally taking a look inside the runtime of an already-compiled running application. You can't just change the code of an application while it's running. So you have to stop debugging if you want to make changes.

You can't swap engines without stopping the car.

 

Link to comment
Share on other sites

  • 0

BTW It makes sense that you're application doesn't work... When you click on Calc, you do CalculateRetail(decWholesale, decMarkup)
But at that point, decWholesale and decMarkup are both 0. Why? Because you never put the values of the textboxes in them.

So you basically saying CalculateRetail(0,0)




 


Also, CBool(False) and CBool(True) are not necessary. You don't need to cast a Boolean (True/False) to a Boolean, and that's what you're doing.
So Return True or Return False should be good enough.
 

Link to comment
Share on other sites

  • 0

I haven't programmed in well over a decade but, during my learning, I was taught that, our mistakes are a lot like Einstein's problem: we know a lot of technical stuff but can't figure out simple stuff.

Link to comment
Share on other sites

  • 0

the cbool part makes sense and i feel stupid for not seeing it sooner.

 

as for inputting the textbox values into decWholesale and DecMarkup....

 

i was thinking that within the btnCalc section of putting

 

        decMarkup = CDec(txtMarkup.Text)
        decWholesale = CDec(txtWholesale.Text)
 
but as i am assuming you know that didnt work. any suggestions to get me on the right path?
Link to comment
Share on other sites

  • 0

it gives me no error. the only errors i get are for this

 

  'is wrong
        'Greater than 0 check
        If (txtMarkup.Text) < 1 Then
            lblWarning.Text = "Markup amount must be numeric and whole number"
            Return CBool(False)
        End If
 
        If (txtWholesale.Text) < 1 Then
            lblWarning.Text = "Wholesale Price must be numeric and whole number"
            Return CBool(False)
 
which of course tells me i can not go from string to double. 
 
 
so it works once i removed the above code, and made the changes noted above. the strange part is it didnt work BEFORE i put in that code either.
Link to comment
Share on other sites

  • 0

so now i still need to make it check if the number is positive. 

 

this is what i have 

Option Strict On
Public Class Form1
 
 
    Private decWholesale As Decimal 'to hold wholesale price
    Private decMarkupPrice As Decimal    'to hold markup %
    Private DecMarkup As Decimal
 
 
 
    Private Function Validatefields() As Boolean
        'is numeric check
        If IsNumeric(decWholesale) Then
            Return CBool(True)
        Else : lblWarning.Text = "Wholesale Price must be numeric"
            Return CBool(False)
        End If
 
        If IsNumeric(DecMarkup) Then
            Return CBool(True)
        Else : lblWarning.Text = "Markup amount must be numeric"
            Return CBool(False)
        End If
 
        If Val(decWholesale) > 0 Then
            lblWarning.Text = "Wholesale Price must be greater than 0"
            Return CBool(False)
        End If
 
        If Val(DecMarkup) > 0 Then
            lblWarning.Text = "Markup amount must be greater than 0"
            Return CBool(False)
        End If
 
      
 
 
        Return CBool(True)
 
    End Function
 
    Function CalculateRetail(ByVal decretail As Decimal, ByVal decMarkup As Decimal) As Decimal
        'calculate and return markup price
        Dim decMarkupPrice As Decimal
        Dim decPercentage As Decimal
 
 
        decPercentage = CDec(decMarkup * 0.01)
        decMarkupPrice = decWholesale + (decretail * decPercentage)
        Return decMarkupPrice
    End Function
 
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
 
 
        DecMarkup = CDec(txtMarkup.Text)
        decWholesale = CDec(txtWholesale.Text)
        'input fields if valid
        If Validatefields() Then
            decMarkupPrice = CalculateRetail(decWholesale, decMarkup)
            lblRetail.Text = decMarkupPrice.ToString("c")
        End If
 
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
 
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'clear previous messages
        lblWarning.Text = String.Empty
        txtWholesale.Text = String.Empty
        txtMarkup.Text = String.Empty
 
 
 
    End Sub
 
End Class
 
   
 

 

 

so, the greater than 0 check works great. however, i have messed up the check to see if it is numeric. i get a popup with this message

 

Additional information: Conversion from string "b" to type 'Decimal' is not valid

 

so this is where i am stuck now. i know i have something messed up with the check. i have also tried having it check txtWholesale and txtMarkup and i still get the same. where should i be looking?

Link to comment
Share on other sites

  • 0

so now i still need to make it check if the number is positive. 

 

this is what i have 

Option Strict On
Public Class Form1
 
 
    Private decWholesale As Decimal 'to hold wholesale price
    Private decMarkupPrice As Decimal    'to hold markup %
    Private DecMarkup As Decimal
 
 
 
    Private Function Validatefields() As Boolean
        'is numeric check
        If IsNumeric(decWholesale) Then
            Return CBool(True)
        Else : lblWarning.Text = "Wholesale Price must be numeric"
            Return CBool(False)
        End If
 
        If IsNumeric(DecMarkup) Then
            Return CBool(True)
        Else : lblWarning.Text = "Markup amount must be numeric"
            Return CBool(False)
        End If
 
        If Val(decWholesale) > 0 Then
            lblWarning.Text = "Wholesale Price must be greater than 0"
            Return CBool(False)
        End If
 
        If Val(DecMarkup) > 0 Then
            lblWarning.Text = "Markup amount must be greater than 0"
            Return CBool(False)
        End If
 
      
 
 
        Return CBool(True)
 
    End Function
 
    Function CalculateRetail(ByVal decretail As Decimal, ByVal decMarkup As Decimal) As Decimal
        'calculate and return markup price
        Dim decMarkupPrice As Decimal
        Dim decPercentage As Decimal
 
 
        decPercentage = CDec(decMarkup * 0.01)
        decMarkupPrice = decWholesale + (decretail * decPercentage)
        Return decMarkupPrice
    End Function
 
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
 
 
        DecMarkup = CDec(txtMarkup.Text)
        decWholesale = CDec(txtWholesale.Text)
        'input fields if valid
        If Validatefields() Then
            decMarkupPrice = CalculateRetail(decWholesale, decMarkup)
            lblRetail.Text = decMarkupPrice.ToString("c")
        End If
 
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
 
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'clear previous messages
        lblWarning.Text = String.Empty
        txtWholesale.Text = String.Empty
        txtMarkup.Text = String.Empty
 
 
 
    End Sub
 
End Class
 
     

 

 

so, the greater than 0 check works great. however, i have messed up the check to see if it is numeric. i get a popup with this message

 

Additional information: Conversion from string "b" to type 'Decimal' is not valid

 

so this is where i am stuck now. i know i have something messed up with the check. i have also tried having it check txtWholesale and txtMarkup and i still get the same. where should i be looking?

I will assist further when I'm home (at work now, so I don't have the time to really take a look at it)

Link to comment
Share on other sites

  • 0

First, your function ValidateFields() doesn't actually validate the fields. It checks that 2 of the private variables you declare at the top are numeric, which they necessarily are because they are Decimals and IsNumeric of a Decimal always returns true. What you probably meant to do was to use IsNumeric on the text boxes where the user can enter some input, then convert these strings to Decimals and finally check if the resulting Decimals are greater than 0.

 

Secondly, in btnCalc_Click, you attempt to convert the text in these textboxes to Decimals before invoking ValidateFields and regardless of whether it returns True or False. Your code in effect assumes that the value is numeric and that the conversion will succeed, only to check afterwards if the values indeed were numeric (which it doesn't really do as I mentioned earlier).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.