question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked ·

Getting the sum of the items in a lstBox

Hello all, I have an annoying issue I can't figure out. I have several numbers in a lstBox that I want to return on a lblTotal. I am using a For...Next type statement. For intCount = 0 To lstCost.Items.Count - 1 Next I've made some attemp by using : 'lblTotal.Text = (+lstCost.Items(intCount).ToString()) ' lblTotal.Text = +declstCost.ToString(intCount).ToString But I cant figure it out.

vs-general
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

This should work:

 lblTotal.Text = "0"
 For intCount = 0 To lstCost.Items.Count - 1
     lblTotal.Text += +lstCost.Items(intCount)
 Next

But if you add the ‘Option Strict On’ line to the beginning of your file to make more reliable programs, then consider this alternative too:

 Dim sum As Integer = 0
 For Each item In lstCost.Items
     Dim n As Integer
     If Integer.TryParse(item.ToString, n) Then sum += n
 Next
 lblTotal.Text = sum.ToString

or something like this:

 lblTotal.Text = lstCost.Items.Cast(Of Object) _
     .Sum(Function(item)
                 Dim n As Integer
                 Integer.TryParse(item.ToString, n)
                 Return n
             End Function).ToString
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'll try that,, I know for certain I should be using the For... Next statement. Option 1 looks like what I may be shooting for.

0 Votes 0 ·
Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 answered ·

the first one worked perfectly, would you be so kind to explain how I can show that total as a currency w/ decimal?

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Check this:

 Dim sum As Decimal = 0
 For intCount = 0 To lstCost.Items.Count - 1
     sum += CDec(lstCost.Items(intCount))
 Next
 lblTotal.Text = sum.ToString("C")


1 Vote 1 ·
Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 answered ·

sum is not declared error

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My bad , simple fix..... Dim sum as decimal

0 Votes 0 ·
Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 answered ·

How can I follow you, you've been very helpful. Are you able to explain certain information when I need help? I am taking my first programming class that revolves around Visual Studio/basics

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 answered ·


     decTotalCost = intNumDays * decLocationFee + decRegistrationFee
     lstCost.Items.Add(decTotalCost).ToString("c")

mt lstCost is not displaying the numbers as ("c") or decimal, any thoughts?

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.