Solved

Trying to SUM listbox items, but getting "Conversion from type 'ListItem' to type 'String' is not valid". Need someone to check code.

Posted on 2013-05-20
3
501 Views
Last Modified: 2013-05-21
Hello,
can someone please advise what I'm doing wrong. I have try many solutions, but just cannot get out from error like:
"Conversion from type 'ListItem' to type 'String' is not valid", or
Conversion from type 'ListItem' to type 'Double' is not valid, or
Conversion from type 'ListItem' to type 'Integer' is not valid, etc

Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        For Each item As String In ListBox1.Items
            sum += Double.Parse(item)
        Next

Open in new window


Thanks in advance for hlep!
0
Comment
Question by:dejandejanovic
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39182638
Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        For Each item As Object In ListBox1.Items
            sum += Double.TryParse(item.ToString())
        Next

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39182646
better yet with TryParse to avoid exception:
Dim result As Double = Math.Round((TextBoxA.Text + TextBoxB.Text, 1)
        MsgBox(result)
        Dim Pcs As String = result * TextBoxC.Text
        MsgBox(Pcs)
        ListBox1.Items.Add(Pcs)
        Dim sum As Double
        Dim number As Double
        For Each item As Object In ListBox1.Items
            if (Double.TryParse(item.ToString(), number)) then
                sum += number
        else
                'parsing failed
        end if
        Next

Open in new window

0
 

Author Closing Comment

by:dejandejanovic
ID: 39183887
Works perfect! Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 40
Expando 4 36
Calling stored proc in EDMX 11 19
Get list of word ducuments in a folder 10 21
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now