|
-
Jul 26th, 2005, 07:25 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] What is wrong with this?
I am BRAND new to programming so please be gentle
I have been working on a calculator of sorts and things are finally progressing but I am encountering two errors that I cannot figure out.
1. If I run the calculator and I fill in both of the text boxes (listing price and sales price) then no errors are encountered, however, if I do not fill in one of the textboxes with a value then I receive:
"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format."
With the error directing to which ever textbox value was left blank. It also says that the code has called into another function and when that function is finished, this is the next function that will be executed.
Here is the code for converting the two text boxes:
VB Code:
Public Function ListFees() As Double
' Compute the initial ebay listing fee
' based on the listing price of the item
listprice = Double.Parse(TextBox1.Text) 'convert input string to number
Select Case listprice
Case 0.0
list = 0.0
Case 0.01 To 0.99
list = 0.25
Case 1 To 9.99
list = (0.35)
Case 10 To 49.99
list = (0.6)
Case 50 To 199.99
list = 2.4
Case 200 To 499.99
list = 3.6
Case Is >= 500
list = 4.8
End Select
End Function
Function FinalValue() As Double
' compute final value fee
' based on the sales price
sellprice = Double.Parse(TextBox2.Text)
Select Case sellprice
Case 0 To 25
fvfee = (sellprice * 0.0525)
Case 25.01 To 1000
fvfee = (sellprice - 25) * 0.0275 + 1.31
Case Is > 1000
fvfee = (sellprice - 1000) * 0.015 + 28.12
End Select
End Function
All variables have previously been declared as Double
2. The second problem is that when the program does run correctly, the decimals are all out of whack. I want all numbers to appear in a currency type of format (two places afer the decimal) but I am getting three or four, unless the second character is a zero, then I only get one digit after the decimal. I am sure I am missing a conversion or something?
Thanks in advance for any help
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 07:32 PM
#2
Thread Starter
Hyperactive Member
Re: What is wrong with this?
Alright, typical that as soon as I ask for help, I get it to work. Can anyone tell me if this a proper way to remedy the problem or if I just kind of rigged it to work?
Plus I still need help with the decimal place proble please.
New code is just the If..Else clause:
VB Code:
Public Function ListFees() As Double
' Compute the initial ebay listing fee
' based on the listing price of the item
If IsNumeric(TextBox1.Text) = False Then
list = 0.0
Else
listprice = Double.Parse(TextBox1.Text) 'convert input string to number
Select Case listprice
Case 0.0
list = 0.0
Case 0.01 To 0.99
list = 0.25
Case 1 To 9.99
list = (0.35)
Case 10 To 49.99
list = (0.6)
Case 50 To 199.99
list = 2.4
Case 200 To 499.99
list = 3.6
Case Is >= 500
list = 4.8
End Select
End If
End Function
Function FinalValue() As Double
' compute final value fee
' based on the sales price
If IsNumeric(TextBox2.Text) = False Then
list = 0.0
Else
sellprice = Double.Parse(TextBox2.Text)
Select Case sellprice
Case 0 To 25
fvfee = (sellprice * 0.0525)
Case 25.01 To 1000
fvfee = (sellprice - 25) * 0.0275 + 1.31
Case Is > 1000
fvfee = (sellprice - 1000) * 0.015 + 28.12
End Select
End If
End Function
Thanks
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 07:49 PM
#3
Re: What is wrong with this?
1. Trying to convert an empty string to a double will throw an exception as it is not a valid format. You can check yourself whether the TextBox is empty but this still doesn't account for the user entering invalid data, like letters. If you are not validating each character as it is entered then I suggest this: check whether the field is empty. If it is then do nothing. If it is not, use Double.TryParse to attempt to convert the text to a Double and display an error message if it fails.
VB Code:
Private Function DoSomething() As Double
Dim listPrice As Double
If Me.TextBox1.Text.Trim() = String.Empty Then
'Do nothing or assume zero as you prefer.
Return 0
ElseIf Not Double.TryParse(Me.TextBox1.Text.Trim(), Globalization.NumberStyles.Any, Nothing, listPrice) Then
'Display appropriate error message.
Return 0
End If
'Use listPrice here, which has been populated in the call to TryParse.
End Function
2. If you are dealing in currency then you should be using the Decimal data type as much as possible. This reduces the risk of round-off error due to floating point arithmetic. Unfortunately you have to use Double in some places because you need to use Double.TryParse. .NET 2.0 has added a Decimal.TryParse method but for now you're stuck with Double. If you want to convert a numerical value to currency, I suggest you use ToString("c"). This will automatically round to the standard number of decimal places for the local currency. I strongly suggest that you accept the default number of decimal places even if there are no cents. Consistency is a good thing. If you remove the cents then the user is not to know whether there are none or you just chose to remove them. If you do want remove the cents when there are none you could use logic like this:
VB Code:
Dim myDecimal As Decimal
Dim myCurrencyString As String
If myDecimal Mod 1 = 0 Then
'Display no decimal places.
myCurrencyString = myDecimal.ToString("c0")
Else
'Display the standard number of decimal places.
myCurrencyString = myDecimal.ToString("c")
End If
Edit:
IsNumeric is a valid method of validation but I, and others, prefer to avoid VB Runtime functions if possible.
-
Jul 26th, 2005, 08:02 PM
#4
Thread Starter
Hyperactive Member
Re: What is wrong with this?
Thanks for the very detailed response. I will take a few minutes to digest what you said and then try to implement it.
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 08:21 PM
#5
Thread Starter
Hyperactive Member
Re: What is wrong with this?
jm,
I implemented the first part of your post and it worked flawlessly, but I am having problems with the decimal part of your post. It seems that no matter where I try to convert a double to a decimal as you suggested, I get parse errors?
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 08:23 PM
#6
Re: What is wrong with this?
I've got a fair idea what the problem is but show me the code you are using so I can be specific.
-
Jul 26th, 2005, 08:38 PM
#7
Thread Starter
Hyperactive Member
Re: What is wrong with this?
jm,
Your assistance is more extremely appreciated, you will note that I actually have not implemented your first suggestion yet either because after further testing, I must have implemented it incorrectly b/c it was failing. The entire project is enclosed in zip.
Last edited by nothingofvalue; Jul 26th, 2005 at 08:58 PM.
Reason: Removed attachment
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 08:51 PM
#8
Re: What is wrong with this?
I'm not a big one for downloading entire projects. Please just post the relevant section of the actual code, i.e. as text in a post.
-
Jul 26th, 2005, 08:57 PM
#9
Thread Starter
Hyperactive Member
Re: What is wrong with this?
Sorry, here it is:
VB Code:
Dim listprice As Double = 0 ' ebay listing price of item
Dim sellprice As Double = 0 ' ebay selling price of item
Dim shipprice As Double = 0 ' shipping fee charged for item
Dim list As Double = 0 ' fee charged by ebay for listing item based on price
Dim fvfee As Double = 0 ' final value fee charged by ebay based on the sales price
Dim option1 As Double = 0 ' option1-4 are add on's to the listing fee for upgraded listing
Dim option2 As Double = 0 '
Dim option3 As Double = 0 '
Dim option4 As Double = 0 '
Dim paypalfee As Double = 0 ' fee charged by paypal if used
Dim reserve As Double = 0 ' additional fee for a reserve price
Public Function ListFees() As Double
' Compute the initial ebay listing fee
' based on the listing price of the item
If IsNumeric(TextBox1.Text) = False Then
list = 0.0
Else
listprice = Double.Parse(TextBox1.Text) 'convert input string to number
Select Case listprice
Case 0.0
list = 0.0
Case 0.01 To 0.99
list = 0.25
Case 1 To 9.99
list = (0.35)
Case 10 To 49.99
list = (0.6)
Case 50 To 199.99
list = 2.4
Case 200 To 499.99
list = 3.6
Case Is >= 500
list = 4.8
End Select
End If
End Function
Public Function reservefee() As Double
If IsNumeric(TextBox4.Text) = False Then
reserve = 0.0
Else
reserve = Double.Parse(TextBox4.Text) 'convert input string to number
Select Case reservefee
Case 0.0
reserve = 0.0
Case 0.01 To 49.99
reserve = 1.0
Case 50 To 199.99
reserve = 2.0
Case Is > 200
reserve = Double.Parse(TextBox4.Text) * 0.01
End Select
End If
End Function
Function FinalValue() As Double
' compute final value fee
' based on the sales price
If IsNumeric(TextBox2.Text) = False Then
list = 0.0
Else
sellprice = Double.Parse(TextBox2.Text)
Select Case sellprice
Case 0 To 25
fvfee = (sellprice * 0.0525)
Case 25.01 To 1000
fvfee = (sellprice - 25) * 0.0275 + 1.31
Case Is > 1000
fvfee = (sellprice - 1000) * 0.015 + 28.12
End Select
End If
End Function
Function Options() As Double
' This section will compute the numeric value of each selected listing option
' each respective value is stored in the variable "option"
' followed by the box number (ie, option1 for combobox1
' compute the numeric value of each option selection combo1
Select Case ComboBox1.Text
Case "Select"
option1 = 0
Case "Gallery Picture"
option1 = 0.35
Case "Listing Designer"
option1 = 0.1
Case "Item Subtitle"
option1 = 0.5
Case "Bold"
option1 = 1
Case "10 Day Auction"
option1 = 0.4
Case "Gift Service"
option1 = 0.25
Case "Border"
option1 = 3
Case "Highlight"
option1 = 5
Case "Featured Plus"
option1 = 19.95
Case "Gallery Featured"
option1 = 19.95
Case "Home Page Featured"
option1 = 39.95
Case "Scheduled Listing"
option1 = 0.1
End Select
' compute the numeric value of each selection combo2
Select Case ComboBox2.Text
Case "Select"
option2 = 0
Case "Gallery Picture"
option2 = 0.35
Case "Listing Designer"
option2 = 0.1
Case "Item Subtitle"
option2 = 0.5
Case "Bold"
option2 = 1
Case "10 Day Auction"
option2 = 0.4
Case "Gift Service"
option2 = 0.25
Case "Border"
option2 = 3
Case "Highlight"
option2 = 5
Case "Featured Plus"
option2 = 19.95
Case "Gallery Featured"
option2 = 19.95
Case "Home Page Featured"
option2 = 39.95
Case "Scheduled Listing"
option2 = 0.1
End Select
' compute the numeric value of each selection combo3
Select Case ComboBox3.Text
Case "Select"
option3 = 0
Case "Gallery Picture"
option3 = 0.35
Case "Listing Designer"
option3 = 0.1
Case "Item Subtitle"
option3 = 0.5
Case "Bold"
option3 = 1
Case "10 Day Auction"
option3 = 0.4
Case "Gift Service"
option3 = 0.25
Case "Border"
option3 = 3
Case "Highlight"
option3 = 5
Case "Featured Plus"
option3 = 19.95
Case "Gallery Featured"
option3 = 19.95
Case "Home Page Featured"
option3 = 39.95
Case "Scheduled Listing"
option3 = 0.1
End Select
' compute the numeric value of each selection combo4
Select Case ComboBox4.Text
Case "Select"
option4 = 0
Case "Gallery Picture"
option4 = 0.35
Case "Listing Designer"
option4 = 0.1
Case "Item Subtitle"
option4 = 0.5
Case "Bold"
option4 = 1
Case "10 Day Auction"
option4 = 0.4
Case "Gift Service"
option4 = 0.25
Case "Border"
option4 = 3
Case "Highlight"
option4 = 5
Case "Featured Plus"
option4 = 19.95
Case "Gallery Featured"
option4 = 19.95
Case "Home Page Featured"
option4 = 39.95
Case "Scheduled Listing"
option4 = 0.1
End Select
End Function
Public Function PaypalFees() As Double
If IsNumeric(TextBox2.Text) = False Then
paypalfee = 0.0
Else
paypalfee = (sellprice + shipprice) * 0.029 + 0.3
End If
End Function
God only knows what else you may find wrong
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 09:25 PM
#10
Re: What is wrong with this?
I thought you were talking about the decimal places in the currency.
I was suggesting that you declare the relevant variables as Decimal and only convert to Double if and when you have to, like this:
VB Code:
'Double.TryParse only accepts a Double.
Dim myDouble As Double
If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Any, Nothing, myDouble) Then
'Convert the parsed Double value to a Decimal for use.
Dim myDecimal As Decimal = CDec(myDouble)
Else
MessageBox.Show("Please enter a valid number.")
End If
This is only a simple example so you would need to adapt it to your needs.
-
Jul 26th, 2005, 09:38 PM
#11
Thread Starter
Hyperactive Member
Re: What is wrong with this?
Not sure if I completely understand what you are suggesting, but i will continue to mess with it. I guess the only way I will learn is to fall down ALOT.
Thank you
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 10:13 PM
#12
Re: What is wrong with this?
I'm suggesting that all the variables that you have declared as Double should be declared as Decimal instead. You don't have to do this but, as I said, it protects you from round-off errors that can occur in floating-point arithmetic, i.e. with Singles and Doubles. You would use Decimal types everywhere except where it was not possible, like the fourth argument to Double.TryParse. In that situation you would have to pass it a Double variable and then convert the Double to a Decimal, e.g. by using CDec, before using it. That way you perform no arithmetic with the Double so you cannot get round-off errors.
-
Jul 26th, 2005, 10:35 PM
#13
Thread Starter
Hyperactive Member
Re: What is wrong with this?
I get what you are saying, the problem I have, because I am pretty clueless, is implementing it. I have studied your examples and I have tried to do something similar in my project, but it's not pretty.
I am really just guessing with it all b/c I do not have enough grasp of all the terminology involved yet. I have been reading and viewing videos on this subject night and day, but it seems like no one actually teaches what you need to know.
I apologize for my lack of knowledge, but I seriously am trying, I just don't get what actually needs to be changed.
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
-
Jul 26th, 2005, 11:05 PM
#14
Re: What is wrong with this?
You don't need to apologise for not having experience. The only way to get it is to do. I do try to use the correct terminology as much as I'm able, which can be a little confusing if you haven't heard it before. Just keep on trying and learn from your mistakes. That's what I did and I'm sure everyone else here is the same. At least you do have places like this to get help. My best advice to you is to not be shy about using the help system provided by the IDE. That's where I've learned the majority of what I know. It can be a little confusing or incomplete at times, so then there's places like this, but the help system should always be your first port of call. You should have worn out your F1 button within the month.
-
Jul 26th, 2005, 11:10 PM
#15
Thread Starter
Hyperactive Member
Re: What is wrong with this?
Thanks for all your help jm
"Imagination is more important than knowledge..."
Albert Einstein
-----------------------------------------------
If my reply helped you then you really were lost, but I still took the time to help, please rate it anyway
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|