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
512 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert datetime to time string 10 28
run a stored procedure from vb.net 1 27
Name Space error VS2015 1 22
ASP.NET Content Page 3 25
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

789 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