thankins Posted October 16, 2003 Posted October 16, 2003 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 Quote
thankins Posted October 16, 2003 Author Posted October 16, 2003 I also forgot to mention that option strict is set to on! Quote
*Experts* Volte Posted October 16, 2003 *Experts* Posted October 16, 2003 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) Quote
thankins Posted October 16, 2003 Author Posted October 16, 2003 What exact is the single.parse command? I looked in my vb . net book and didnt see anything. Do you mind explaining it Quote
Administrators PlausiblyDamp Posted October 16, 2003 Administrators Posted October 16, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Gurus* Derek Stone Posted October 16, 2003 *Gurus* Posted October 16, 2003 Books really aren't a complete reference source. Use MSDN instead. [msdn=System.Single]System.Single.Parse()[/msdn] Quote Posting Guidelines
thankins Posted October 16, 2003 Author Posted October 16, 2003 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)) Quote
thankins Posted October 16, 2003 Author Posted October 16, 2003 Thank you Derek, I have added that site to my favorites and will be checking it out! Quote
*Experts* Volte Posted October 16, 2003 *Experts* Posted October 16, 2003 Right before that line, put this:MessageBox.Show(txtFederal.Text.SubString(0, txtFederal.Text.Length - 1))and post here what it says. Quote
thankins Posted October 16, 2003 Author Posted October 16, 2003 It just says "." or if i enter .05 it says ".0" Is this because i am entering a smaller amount then 1? Quote
*Experts* Volte Posted October 16, 2003 *Experts* Posted October 16, 2003 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. Quote
thankins Posted October 16, 2003 Author Posted October 16, 2003 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. Quote
*Experts* Volte Posted October 16, 2003 *Experts* Posted October 16, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.