#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    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:

    Code:
    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
            Else
                If num > 100 Then
                    cost = 0.03 * num '3 cents for additional copies
                End If
            End If
                txtCost.Text = cost.ToString("C")
        End Sub
    End Class
    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!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    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.

    J.A. Coutts
    Code:
    Private Sub cmdCalc_Click()
        Dim Cost As Currency
        If IsNumeric(txtNumCopy.Text) Then
            If Val(txtNumCopy) <= 100 Then
                Cost = txtNumCopy.Text * 0.05
            Else
                Cost = 100 * 0.05 + (txtNumCopy.Text - 100) * 0.03
            End If
        Else
            MsgBox "Number of copies is not a numeric value!", vbExclamation
            Exit Sub
        End If
        txtCost.Text = Format(Cost, "0.00")
    End Sub
    Last edited by couttsj; October 19th, 2013 at 12:04 PM.

IMN logo majestic logo threadwatch logo seochat tools logo