October 19th, 2013, 02:44 AM
New User In Need Of Some Help on VS 2012
Okay, so I'm taking this class that works with the program and one of my assignments is absolutely killing me right now. I'm new so odds are I'll probably be coming here to ask for help a lot, just saying. But anyways, back to my problem.
For one of my assignments, I have to write a program that charges $0.05 for the first 100 copies and $0.03 per each additional copy. So here's the code I came up with so far:
Now I've bolded where I think I'm having the biggest problem so I have no idea how to come up with the right equation to come up with the solution that gives me 125 copies for $5.75. Anyone care to tell me where my code is faulty? Thanks for any help!!
Public Class Form1
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim num, cost As Double
num = CDbl(txtNumCopy.Text)
If num = 100 Then
cost = 0.05 * num '5 cents for first 100 copies
If num > 100 Then
cost = 0.03 * num '3 cents for additional copies
txtCost.Text = cost.ToString("C")
October 19th, 2013, 11:44 AM
What you have to understand is that VB Controls use Variants which are coerced into whatever is required for the calculation. So there is a lot of redundancy in your code. As well, you should check that it is actually numeric before attempting a calculation.
Private Sub cmdCalc_Click()
Dim Cost As Currency
If IsNumeric(txtNumCopy.Text) Then
If Val(txtNumCopy) <= 100 Then
Cost = txtNumCopy.Text * 0.05
Cost = 100 * 0.05 + (txtNumCopy.Text - 100) * 0.03
MsgBox "Number of copies is not a numeric value!", vbExclamation
txtCost.Text = Format(Cost, "0.00")
Last edited by couttsj; October 19th, 2013 at 12:04 PM.