Unhandled Expections?!?!

thankins

Member
Joined
Sep 24, 2003
Messages
24
I recieve the following message when i try to compile my program, and I dont know what it means!

An unhandled exception of type System.InvalidCastException occurred in microsoft.visualbasic.dll

Additional information: Cast from string "10%" to type Single is not valid.

I am trying to make it so when the user enters a number with the percent sign (10%) into my text box the program will treat it like 10.0

Here are parts of my code

Code:
    Private Sub txtFederal_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFederal.Validating
        Vaildates that a number has been entered into the text box

        If Not IsNumeric(Val(txtFederal.Text)) Then
            MessageBox.Show("Federal taxes must be a number.", "Invaild Input", MessageBoxButtons.OK, _
            MessageBoxIcon.Error)

            Select the existing text in the text box.
            txtFederal.SelectionStart = 0
            txtFederal.SelectionLength = txtFederal.Text.Length
            Set the e.Cacel to true so the focus will stay in the control
            e.Cancel = True
        Else
            e.Cancel = False
        End If
    End Sub

and here is the part where the error is occuring
Code:
    Sub InputData(ByRef empName As String, ByRef hrRate As Single, ByRef hrsWorked As Single, ByRef fedTax As Single, ByRef ficaTax As Single, ByRef stateTax As Single)

         Get payroll data for employee
        empName = txtName.Text
        hrRate = CInt(txtRate.Text)
        hrsWorked = CInt(txtHours.Text)
       [COLOR=blue]fedTax = CSng(txtFederal.Text)[/COLOR] 
        ficaTax = CSng(txtFica.Text)
        stateTax = CSng(txtState.Text)

The bluw text above is where the error is occuring
 
You need to strip off the % in order for it to parse the number correctly. For example:
Code:
fedTax = Single.Parse(txtFederal.Text.SubString(0, txtFederal.Text.Length - 1)
 
What exact is the single.parse command? I looked in my vb . net book and didnt see anything. Do you mind explaining it
 
Single.Parse is a shared function of the Single DataType. It will parse a string and return a Single. Similar functions exist for Integer, Double etc.
 
I put it into my code and now i recieve a new error

An unhandled exception of type System.FormatException occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

I copied it from what was posted

Code:
   fedTax = Single.Parse(txtFederal.Text.Substring(0, txtFederal.Text.Length - 1))
 
Right before that line, put this:
Code:
MessageBox.Show(txtFederal.Text.SubString(0, txtFederal.Text.Length - 1))
and post here what it says.
 
But I thought you wanted to be able to enter in a % sign? Try this, then:
Code:
If txtFederal.Text.EndsWith("%") Then fedTax = Single.Parse(txtFederal.Text.Substring(0, txtFederal.Text.Length - 1))
The problem is that its removing the last character (which I assumed would be a %). If its not always going to be % you need to check, which is what my code above does.
 
Yes I do, but I want to have a way so that if the user doesnt enters the percent sign the program wont crash, so i should use the if statement then correct?!

Also while we are on the topic of percent, how would you go about turning the number they enter into a percent? Because now how I have the program set up it just treats it as a whole number

Code:
  Function Fed_Tax(ByRef grossPay As Single, ByRef fedTax As Single) As Single
        Compute amount to be taken for Federal Tax
        Fed_Tax = grossPay * fedTax

I thought about adding a ".01 to the fed tax so that it reads

Code:
   Function Fed_Tax(ByRef grossPay As Single, ByRef fedTax As Single) As Single
        Compute amount to be taken for Federal Tax
        Fed_Tax = grossPay * (fedTax * .01)

But i didnt know if that was "good programming" or not.
 
1) Yes, the code in my previous post will allow you to put a percent sign or not put one. You may even want to trim it so no extra spaces will throw it off:
Code:
Dim sTax As String = txtFederal.Text.Trim() added the variable for cleanliness, and the Trim() 
                                            call to remove any spaces that are padding the number.

fedTax = Single.Parse(sTax.Substring(0, sTax.Length - 1))

2) Yes, thats the way you do it.
 
Back
Top