[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

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

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
dejandejanovic
Asked:
dejandejanovic
  • 2
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
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
 
dejandejanovicAuthor Commented:
Works perfect! Thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now