Results 1 to 15 of 15

Thread: [RESOLVED] What is wrong with this?

  1. #1

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    Resolved [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:
    1. Public Function ListFees() As Double
    2.  
    3.  
    4.         '   Compute the initial ebay listing fee
    5.         '   based on the listing price of the item
    6.  
    7.        
    8.  
    9.         listprice = Double.Parse(TextBox1.Text)  'convert input string to number
    10.  
    11.  
    12.  
    13.         Select Case listprice
    14.             Case 0.0
    15.                 list = 0.0
    16.             Case 0.01 To 0.99
    17.                 list = 0.25
    18.             Case 1 To 9.99
    19.                 list = (0.35)
    20.             Case 10 To 49.99
    21.                 list = (0.6)
    22.             Case 50 To 199.99
    23.                 list = 2.4
    24.             Case 200 To 499.99
    25.                 list = 3.6
    26.             Case Is >= 500
    27.                 list = 4.8
    28.         End Select
    29.  
    30.  
    31.  
    32.  
    33.     End Function
    34.     Function FinalValue() As Double
    35.  
    36.         '   compute final value fee
    37.         '   based on the sales price
    38.  
    39.  
    40.         sellprice = Double.Parse(TextBox2.Text)
    41.  
    42.         Select Case sellprice
    43.  
    44.             Case 0 To 25
    45.                 fvfee = (sellprice * 0.0525)
    46.             Case 25.01 To 1000
    47.                 fvfee = (sellprice - 25) * 0.0275 + 1.31
    48.             Case Is > 1000
    49.                 fvfee = (sellprice - 1000) * 0.015 + 28.12
    50.         End Select
    51.  
    52.     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

  2. #2

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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:
    1. Public Function ListFees() As Double
    2.  
    3.  
    4.         '   Compute the initial ebay listing fee
    5.         '   based on the listing price of the item
    6.  
    7.         If IsNumeric(TextBox1.Text) = False Then
    8.             list = 0.0
    9.         Else
    10.  
    11.  
    12.         listprice = Double.Parse(TextBox1.Text)  'convert input string to number
    13.  
    14.  
    15.  
    16.         Select Case listprice
    17.             Case 0.0
    18.                 list = 0.0
    19.             Case 0.01 To 0.99
    20.                 list = 0.25
    21.             Case 1 To 9.99
    22.                 list = (0.35)
    23.             Case 10 To 49.99
    24.                 list = (0.6)
    25.             Case 50 To 199.99
    26.                 list = 2.4
    27.             Case 200 To 499.99
    28.                 list = 3.6
    29.             Case Is >= 500
    30.                 list = 4.8
    31.         End Select
    32.         End If
    33.  
    34.  
    35.  
    36.     End Function
    37.     Function FinalValue() As Double
    38.  
    39.         '   compute final value fee
    40.         '   based on the sales price
    41.         If IsNumeric(TextBox2.Text) = False Then
    42.             list = 0.0
    43.         Else
    44.  
    45.  
    46.             sellprice = Double.Parse(TextBox2.Text)
    47.  
    48.             Select Case sellprice
    49.  
    50.                 Case 0 To 25
    51.                     fvfee = (sellprice * 0.0525)
    52.                 Case 25.01 To 1000
    53.                     fvfee = (sellprice - 25) * 0.0275 + 1.31
    54.                 Case Is > 1000
    55.                     fvfee = (sellprice - 1000) * 0.015 + 28.12
    56.             End Select
    57.         End If
    58.  
    59.     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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Function DoSomething() As Double
    2.         Dim listPrice As Double
    3.  
    4.         If Me.TextBox1.Text.Trim() = String.Empty Then
    5.             'Do nothing or assume zero as you prefer.
    6.             Return 0
    7.         ElseIf Not Double.TryParse(Me.TextBox1.Text.Trim(), Globalization.NumberStyles.Any, Nothing, listPrice) Then
    8.             'Display appropriate error message.
    9.             Return 0
    10.         End If
    11.  
    12.         'Use listPrice here, which has been populated in the call to TryParse.
    13.     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:
    1. Dim myDecimal As Decimal
    2.         Dim myCurrencyString As String
    3.  
    4.         If myDecimal Mod 1 = 0 Then
    5.             'Display no decimal places.
    6.             myCurrencyString = myDecimal.ToString("c0")
    7.         Else
    8.             'Display the standard number of decimal places.
    9.             myCurrencyString = myDecimal.ToString("c")
    10.         End If

    Edit:
    IsNumeric is a valid method of validation but I, and others, prefer to avoid VB Runtime functions if possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  5. #5

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    Smile Re: What is wrong with this?

    Sorry, here it is:

    VB Code:
    1. Dim listprice As Double = 0    '   ebay listing price of item
    2.     Dim sellprice As Double = 0    '   ebay selling price of item
    3.     Dim shipprice As Double = 0    '   shipping fee charged for item
    4.     Dim list As Double = 0         '   fee charged by ebay for listing item based on price
    5.     Dim fvfee As Double = 0        '   final value fee charged by ebay based on the sales price
    6.     Dim option1 As Double = 0      '   option1-4 are add on's to the listing fee for upgraded listing
    7.     Dim option2 As Double = 0      '
    8.     Dim option3 As Double = 0      '
    9.     Dim option4 As Double = 0      '
    10.     Dim paypalfee As Double = 0    '   fee charged by paypal if used
    11.     Dim reserve As Double = 0      '   additional fee for a reserve price
    12.  
    13.  
    14.    
    15.  
    16.  
    17.  
    18.     Public Function ListFees() As Double
    19.  
    20.  
    21.         '   Compute the initial ebay listing fee
    22.         '   based on the listing price of the item
    23.  
    24.         If IsNumeric(TextBox1.Text) = False Then
    25.             list = 0.0
    26.         Else
    27.  
    28.  
    29.             listprice = Double.Parse(TextBox1.Text)  'convert input string to number
    30.  
    31.  
    32.  
    33.             Select Case listprice
    34.                 Case 0.0
    35.                     list = 0.0
    36.                 Case 0.01 To 0.99
    37.                     list = 0.25
    38.                 Case 1 To 9.99
    39.                     list = (0.35)
    40.                 Case 10 To 49.99
    41.                     list = (0.6)
    42.                 Case 50 To 199.99
    43.                     list = 2.4
    44.                 Case 200 To 499.99
    45.                     list = 3.6
    46.                 Case Is >= 500
    47.                     list = 4.8
    48.         End Select
    49.  
    50.         End If
    51.  
    52.  
    53.     End Function
    54.     Public Function reservefee() As Double
    55.  
    56.         If IsNumeric(TextBox4.Text) = False Then
    57.             reserve = 0.0
    58.         Else
    59.  
    60.  
    61.             reserve = Double.Parse(TextBox4.Text)  'convert input string to number
    62.  
    63.  
    64.  
    65.             Select Case reservefee
    66.                 Case 0.0
    67.                     reserve = 0.0
    68.                 Case 0.01 To 49.99
    69.                     reserve = 1.0
    70.                 Case 50 To 199.99
    71.                     reserve = 2.0
    72.                 Case Is > 200
    73.                     reserve = Double.Parse(TextBox4.Text) * 0.01
    74.             End Select
    75.         End If
    76.     End Function
    77.  
    78.     Function FinalValue() As Double
    79.  
    80.         '   compute final value fee
    81.         '   based on the sales price
    82.         If IsNumeric(TextBox2.Text) = False Then
    83.             list = 0.0
    84.         Else
    85.  
    86.  
    87.             sellprice = Double.Parse(TextBox2.Text)
    88.  
    89.             Select Case sellprice
    90.  
    91.                 Case 0 To 25
    92.                     fvfee = (sellprice * 0.0525)
    93.                 Case 25.01 To 1000
    94.                     fvfee = (sellprice - 25) * 0.0275 + 1.31
    95.                 Case Is > 1000
    96.                     fvfee = (sellprice - 1000) * 0.015 + 28.12
    97.         End Select
    98.         End If
    99.  
    100.     End Function
    101.  
    102.  
    103.  
    104.  
    105.     Function Options() As Double
    106.  
    107.         '   This section will compute the numeric value of each selected listing option
    108.         '   each respective value is stored in the variable "option"
    109.         '   followed by the box number (ie, option1 for combobox1
    110.  
    111.  
    112.         '   compute the numeric value of each option selection combo1
    113.  
    114.  
    115.  
    116.         Select Case ComboBox1.Text
    117.             Case "Select"
    118.                 option1 = 0
    119.             Case "Gallery Picture"
    120.                 option1 = 0.35
    121.             Case "Listing Designer"
    122.                 option1 = 0.1
    123.             Case "Item Subtitle"
    124.                 option1 = 0.5
    125.             Case "Bold"
    126.                 option1 = 1
    127.             Case "10 Day Auction"
    128.                 option1 = 0.4
    129.             Case "Gift Service"
    130.                 option1 = 0.25
    131.             Case "Border"
    132.                 option1 = 3
    133.             Case "Highlight"
    134.                 option1 = 5
    135.             Case "Featured Plus"
    136.                 option1 = 19.95
    137.             Case "Gallery Featured"
    138.                 option1 = 19.95
    139.             Case "Home Page Featured"
    140.                 option1 = 39.95
    141.             Case "Scheduled Listing"
    142.                 option1 = 0.1
    143.         End Select
    144.  
    145.  
    146.         '   compute the numeric value of each selection combo2
    147.  
    148.  
    149.  
    150.  
    151.         Select Case ComboBox2.Text
    152.             Case "Select"
    153.                 option2 = 0
    154.             Case "Gallery Picture"
    155.                 option2 = 0.35
    156.             Case "Listing Designer"
    157.                 option2 = 0.1
    158.             Case "Item Subtitle"
    159.                 option2 = 0.5
    160.             Case "Bold"
    161.                 option2 = 1
    162.             Case "10 Day Auction"
    163.                 option2 = 0.4
    164.             Case "Gift Service"
    165.                 option2 = 0.25
    166.             Case "Border"
    167.                 option2 = 3
    168.             Case "Highlight"
    169.                 option2 = 5
    170.             Case "Featured Plus"
    171.                 option2 = 19.95
    172.             Case "Gallery Featured"
    173.                 option2 = 19.95
    174.             Case "Home Page Featured"
    175.                 option2 = 39.95
    176.             Case "Scheduled Listing"
    177.                 option2 = 0.1
    178.         End Select
    179.  
    180.         '   compute the numeric value of each selection combo3
    181.  
    182.  
    183.  
    184.         Select Case ComboBox3.Text
    185.             Case "Select"
    186.                 option3 = 0
    187.             Case "Gallery Picture"
    188.                 option3 = 0.35
    189.             Case "Listing Designer"
    190.                 option3 = 0.1
    191.             Case "Item Subtitle"
    192.                 option3 = 0.5
    193.             Case "Bold"
    194.                 option3 = 1
    195.             Case "10 Day Auction"
    196.                 option3 = 0.4
    197.             Case "Gift Service"
    198.                 option3 = 0.25
    199.             Case "Border"
    200.                 option3 = 3
    201.             Case "Highlight"
    202.                 option3 = 5
    203.             Case "Featured Plus"
    204.                 option3 = 19.95
    205.             Case "Gallery Featured"
    206.                 option3 = 19.95
    207.             Case "Home Page Featured"
    208.                 option3 = 39.95
    209.             Case "Scheduled Listing"
    210.                 option3 = 0.1
    211.         End Select
    212.  
    213.  
    214.         '   compute the numeric value of each selection combo4
    215.  
    216.  
    217.  
    218.  
    219.         Select Case ComboBox4.Text
    220.             Case "Select"
    221.                 option4 = 0
    222.             Case "Gallery Picture"
    223.                 option4 = 0.35
    224.             Case "Listing Designer"
    225.                 option4 = 0.1
    226.             Case "Item Subtitle"
    227.                 option4 = 0.5
    228.             Case "Bold"
    229.                 option4 = 1
    230.             Case "10 Day Auction"
    231.                 option4 = 0.4
    232.             Case "Gift Service"
    233.                 option4 = 0.25
    234.             Case "Border"
    235.                 option4 = 3
    236.             Case "Highlight"
    237.                 option4 = 5
    238.             Case "Featured Plus"
    239.                 option4 = 19.95
    240.             Case "Gallery Featured"
    241.                 option4 = 19.95
    242.             Case "Home Page Featured"
    243.                 option4 = 39.95
    244.             Case "Scheduled Listing"
    245.                 option4 = 0.1
    246.         End Select
    247.  
    248.  
    249.  
    250.  
    251.  
    252.     End Function
    253.     Public Function PaypalFees() As Double
    254.         If IsNumeric(TextBox2.Text) = False Then
    255.             paypalfee = 0.0
    256.         Else
    257.  
    258.             paypalfee = (sellprice + shipprice) * 0.029 + 0.3
    259.         End If
    260.     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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 'Double.TryParse only accepts a Double.
    2. Dim myDouble As Double
    3.  
    4. If Double.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.Any, Nothing, myDouble) Then
    5.     'Convert the parsed Double value to a Decimal for use.
    6.     Dim myDecimal As Decimal = CDec(myDouble)
    7. Else
    8.     MessageBox.Show("Please enter a valid number.")
    9. End If
    This is only a simple example so you would need to adapt it to your needs.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member nothingofvalue's Avatar
    Join Date
    Jul 2005
    Location
    Arizona
    Posts
    489

    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
  •  



Click Here to Expand Forum to Full Width