|
-
Oct 11th, 2021, 08:08 AM
#1
Thread Starter
Lively Member
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
-
Oct 11th, 2021, 08:17 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 08:17 AM
#3
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:
If Integer.TryParse(tb.Text, Val) Then
Sum += Val
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:
Integer.TryParse(tb.Text, Val)
Sum += Val
-
Oct 11th, 2021, 08:20 AM
#4
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.
-
Oct 11th, 2021, 08:20 AM
#5
Thread Starter
Lively Member
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.
-
Oct 11th, 2021, 08:22 AM
#6
Re: ARRAY summing issue
 Originally Posted by .paul.
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.
-
Oct 11th, 2021, 08:34 AM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 08:38 AM
#8
Re: ARRAY summing issue
@jm…
 Originally Posted by K3JAE
ALL items in the array have a value in currency form (decimal) - some of them will have a value of '0.00'.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 09:05 AM
#9
Thread Starter
Lively Member
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.
-
Oct 11th, 2021, 09:34 AM
#10
Re: ARRAY summing issue
Code:
If Decimal.TryParse(tb.Text, Globalization.NumberStyles.AllowLeadingSign Or Globalization.NumberStyles.Number, Globalization.CultureInfo.CurrentCulture, Val) Then
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 09:48 AM
#11
Thread Starter
Lively Member
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.
-
Oct 11th, 2021, 09:51 AM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 10:06 AM
#13
Thread Starter
Lively Member
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.
-
Oct 11th, 2021, 10:11 AM
#14
Re: ARRAY summing issue
Remove the currency symbols from the textboxes and remove allowcurrencysymbol
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 10:40 AM
#15
Thread Starter
Lively Member
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!
-
Oct 11th, 2021, 10:43 AM
#16
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
 
-
Oct 11th, 2021, 10:55 AM
#17
Re: ARRAY summing issue
 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???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2021, 10:58 AM
#18
Thread Starter
Lively Member
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).
-
Oct 11th, 2021, 11:05 AM
#19
-
Oct 11th, 2021, 11:32 AM
#20
Thread Starter
Lively Member
Re: ARRAY summing issue
Totally understand what your saying... !!
-
Oct 11th, 2021, 12:36 PM
#21
Re: ARRAY summing issue
 Originally Posted by Shaggy Hiker
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
-
Oct 11th, 2021, 02:25 PM
#22
Re: ARRAY summing issue
 Originally Posted by passel
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)
-
Oct 11th, 2021, 07:16 PM
#23
Re: ARRAY summing issue
 Originally Posted by .paul.
@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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|