Results 1 to 23 of 23

Thread: ARRAY summing issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Exclamation ARRAY summing issue

    I cannot get this piece of code to give me a sum - it is only returning '0.00.' Can someone please look at it and tell me where I am dropping the ball?

    ALL items in the array have a value in currency form (decimal) - some of them will have a value of '0.00'.

    Code:
        Private Sub TaxDue()
            ' Creates Array of specific Textboxes to sum
            Dim IFTAdue() As TextBox = {tbTaxDueAL, tbTaxDueAK, tbTaxDueAZ, tbTaxDueAR, tbTaxDueCA, tbTaxDueCO, tbTaxDueCT, tbTaxDueDE, tbTaxDueDC, tbTaxDueFL, tbTaxDueGA, tbTaxDueHI,
                                        tbTaxDueID, tbTaxDueIL, tbTaxDueIN, tbTaxDueIA, tbTaxDueKS, tbTaxDueKY, tbTaxDueLA, tbTaxDueME, tbTaxDueMD, tbTaxDueMA, tbTaxDueMI, tbTaxDueMN,
                                        tbTaxDueMS, tbTaxDueMO, tbTaxDueMT, tbTaxDueNE, tbTaxDueNV, tbTaxDueNH, tbTaxDueNJ, tbTaxDueNM, tbTaxDueNY, tbTaxDueNC, tbTaxDueND, tbTaxDueOH,
                                        tbTaxDueOK, tbTaxDueOR, tbTaxDuePA, tbTaxDueRI, tbTaxDueSC, tbTaxDueSD, tbTaxDueTN, tbTaxDueTX, tbTaxDueUT, tbTaxDueVT, tbTaxDueVA, tbTaxDueWA,
                                        tbTaxDueWV, tbTaxDueWI, tbTaxDueWY}
    
            Dim Sum As Integer = 0
            Dim Val As Integer = 0
            For Each tb As TextBox In IFTAdue
    
                If Integer.TryParse(tb.Text, Val) Then
                    Sum += Convert.ToInt32(tb.Text)
                End If
            Next
    
            tbIFTADue.Text = Sum.ToString("C2")
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    Code:
    Dim Sum As Decimal = 0
    Dim Val As Decimal = 0
    For Each tb As TextBox In IFTAdue
        If Decimal.TryParse(tb.Text, Val) Then
            Sum += val
        End If
    Next

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

    Re: ARRAY summing issue

    Firstly, this part is pointless:
    Code:
    Sum += Convert.ToInt32(tb.Text)
    At that point, the text has already been converted. That's what the Val variable is for in your code. If the attempt to parse the text is successful, Val contains the result. As such, you can just do this:
    vb.net Code:
    1. If Integer.TryParse(tb.Text, Val) Then
    2.     Sum += Val
    3. End If
    In fact, you don't even need the If statement, because Val will contain zero if the attempt to convert the text fails. That means that, if you like, you can just do this:
    vb.net Code:
    1. Integer.TryParse(tb.Text, Val)
    2. Sum += Val

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: ARRAY summing issue

    As for the issue, nothing really jumps out as being a specific problem. You need to debug your code. That's why VS has a debugger. Set a breakpoint at the top of the code and then step through it line by line, examining the state at each step and observing the course of execution. You can see what text the TextBox contains on each iteration and, if the conversion fails when you think it should succeed, you can relay to us what the actual data was that it failed on. We're then not left to guess.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    Appreciate the quick response.

    However your adjusted offering still results in a result of '0.00'.

    Being arrays are a serious weakness for me, I do see the values in the array. What happens in debug as I see it the issue is at this point:

    Decimal.TryParse(tb.Text, Val)

    tb.text shows the proper value(s) ... however VAL is always showing '0' so val is sending '0' to sum all the time.
    Last edited by K3JAE; Oct 11th, 2021 at 08:46 AM.

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

    Re: ARRAY summing issue

    Quote Originally Posted by .paul. View Post
    Code:
    Dim Sum As Decimal = 0
    Dim Val As Decimal = 0
    For Each tb As TextBox In IFTAdue
        If Decimal.TryParse(tb.Text, Val) Then
            Sum += val
        End If
    Next
    That doesn't necessarily help. If the user is supposed to be entering whole dollar values, for instance, the result won't be affected by using Integer or Decimal and, in fact, using Decimal would actually produce the wrong result by including values that weren't whole numbers.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    Code:
    Dim Sum As Decimal = 0
    Dim Val As Decimal = 0
    For Each tb As TextBox In IFTAdue
        If Decimal.TryParse(tb.Text, Globalization.NumberStyles.Number Or Globalization.NumberStyles.AllowCurrencySymbol, Globalization.CultureInfo.CurrentCulture, Val) Then
            Sum += val
        End If
    Next

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    @jm…

    Quote Originally Posted by K3JAE View Post
    ALL items in the array have a value in currency form (decimal) - some of them will have a value of '0.00'.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    OK, Paul;

    Your 2nd solution at least brings back a value. However the expected value should be -$371.01 and I am returning back +$641.91

    There are many Negative numbers (sorry, should have advised that as well) - so summing both positive and negative numbers should result in the proper calculation.

    For the specific dataset I am using the data is as follows:

    374.83
    -182.30
    -503.75
    -8.94
    -164.95
    267.08
    -65.98
    -87.00
    -371.01
    (all remaining tb are 0.00)

    So, in a nutshell it is ignoring the negative numbers ( or more correctly looking at the negative number as a '0' ) and summing only the 2 positive numbers above which equates to the incorrect sum value being returned (374.83 + 267.08 - 641.91).
    Last edited by K3JAE; Oct 11th, 2021 at 09:30 AM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    Code:
    If Decimal.TryParse(tb.Text, Globalization.NumberStyles.AllowLeadingSign Or Globalization.NumberStyles.Number, Globalization.CultureInfo.CurrentCulture, Val) Then

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    The above, as sent, produced a '$0.00'

    Added back into the new offer 'Globalization.NumberStyles.AllowCurrencySymbol' still came back with $641.91 i.e. still not using the negative numbers.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    I tried with both allowleadingsign and allowcurrencysymbol.
    I also couldn’t get it working with both. Without the currency symbols, it’ll work

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    Taking away allowcurrencysymbol brings back a 0.00 as the result. Again, it appears it is totally ignoring the negative values or assigning a 0 instead of the true negative value. Using the numbers given above, the SUM should actually be a negative whereas it is giving 0 as it's result instead of the actual negative value of -371.01.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    Remove the currency symbols from the textboxes and remove allowcurrencysymbol

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    That resolved the issue. Many thanks Paul. Well done.

    Sorry I am not very good at manipulating arrays... I'm still trying to figure all the nuances out!

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: ARRAY summing issue

    One other change you should probably make, though it shouldn't have any impact in this situation, is that you shouldn't have a variable named Val. As it happens, Val() is a function, and a function that would cause trouble similar to the trouble you are having. The compiler is smart enough to realize that you aren't calling the function, but are using Val as a variable, still it's not great practice to have a variable named that way....and terribly ironic that you happened to name the variable that is causing you trouble with the name of a function that would cause the trouble you were having, if you had used it in place of the TryParse function.

    Another point, which may be too late, is that you could solve this whole thing if you had used NumericUpDown rather than textboxes. Textboxes ONLY hold strings, whereas NUD ONLY hold numbers, thereby making them somewhat more appropriate for this use. The .Value property of the NUD is a Decimal already, so there is no conversion needed. All you'd have to do is set the decimal places to 2, and set the max and minimum to whatever values you want. Using a NUD in place of a textbox would have removed this issue, since the issue ultimately comes down to a failure to convert the string from a textbox into the correct number.

    Other than that, I would be putting a breakpoint on the If statement and seeing what the TryParse method was returning for the values that are going bad.

    And on a different note, how do you end up with a negative tax due in a state? I've never managed that.
    My usual boring signature: Nothing

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: ARRAY summing issue

    Quote Originally Posted by Shaggy Hiker
    And on a different note, how do you end up with a negative tax due in a state? I've never managed that.
    Tax rebate???

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    Shaggy

    Thank you for the additional information and will look to incorporate those ideas....

    In relation to your negative tax in a state - this is for Truckers who have to pay fuel tax even if they purchase no fuel in the state they are passing through... in other words you cannot drive a semi even a foot into any state without paying for it. The negative numbers are states where we purchased no fuel (like California as an example) due to their extreme and outrageous cost of fuel. Yet, because we made a few trips into that god-forsaken hole we are required to pay the fuel tax as if we purchased fuel. Where we did purchase fuel and bought more than was required to meet the minimum fuel tax we get a 'credit' (aka positive numbers) which is then spread around to those states we did not purchase fuel in where we traveled (aka California).

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: ARRAY summing issue

    Watch out. There are a lot of Europeans on this forum. Whenever anybody in the US starts bellyaching about fuel costs, they jump in to discuss their fuel costs and show us that our worst cases are really good in comparison
    My usual boring signature: Nothing

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: ARRAY summing issue

    Totally understand what your saying... !!

  21. #21
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: ARRAY summing issue

    Quote Originally Posted by Shaggy Hiker View Post
    Watch out. There are a lot of Europeans on this forum. Whenever anybody in the US starts bellyaching about fuel costs, they jump in to discuss their fuel costs and show us that our worst cases are really good in comparison
    Exactly. Is is really a surprise that with fuel costing $12 a gallon in Norway that Electric cars made up over 70 percent of the new vehicles sold in Norway this year.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  22. #22
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: ARRAY summing issue

    Quote Originally Posted by passel View Post
    Exactly. Is is really a surprise that with fuel costing $12 a gallon in Norway that Electric cars made up over 70 percent of the new vehicles sold in Norway this year.
    we are at 7$ a gallon at best in France so electric driving is not fully spread
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: ARRAY summing issue

    Quote Originally Posted by .paul. View Post
    @jm…
    Totally missed that.

Tags for this Thread

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