Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

 

   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

 
   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

  • *Experts*
Posted
You need to strip off the % in order for it to parse the number correctly. For example:
fedTax = Single.Parse(txtFederal.Text.SubString(0, txtFederal.Text.Length - 1)

Posted

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

 

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

  • *Experts*
Posted
Right before that line, put this:
MessageBox.Show(txtFederal.Text.SubString(0, txtFederal.Text.Length - 1))

and post here what it says.

  • *Experts*
Posted
But I thought you wanted to be able to enter in a % sign? Try this, then:
If txtFederal.Text.EndsWith("%") Then fedTax = Single.Parse(txtFederal.Text.Substring(0, txtFederal.Text.Length - 1))

The problem is that it's removing the last character (which I assumed would be a %). If it's not always going to be % you need to check, which is what my code above does.

Posted

Yes I do, but I want to have a way so that if the user doesnt enters the percent sign the program won't 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

 

 
 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

 

   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.

  • *Experts*
Posted

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:

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, that's the way you do it.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...