locked
copy listbox to array RRS feed

  • Question

  • User282693488 posted

    I an trying to copy the contents of a listbox to an array, and when I do I get this error message (At least one element in the source array could not be cast down to the destination array type) I am using asp.net 3.5 and VB. The code to trigger this is

     Sub
    button7_click(ByVal Source As Object, ByVal E As EventArgs)

    Dim s(Listbox2.Items.Count) As String

    Dim al As New ArrayList

    Listbox2.Items.CopyTo(s, 0)

    al = New ArrayList(s)

    The code for listbox2 is

    <asp:ListBox id="Listbox2" width="200px" height="299" SelectionMode="Multiple"

    runat="server" Visible="False" /></td>    

    Can someone please help me get around this?

    Sunday, November 9, 2008 8:49 AM

Answers

  • User-158764254 posted

    try it like this:

            Dim al As New ArrayList
            For Each li As ListItem In Me.ListBox2.Items
                al.Add(li.Value)
            Next
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2008 10:30 AM

All replies

  • User-158764254 posted

    try it like this:

            Dim al As New ArrayList
            For Each li As ListItem In Me.ListBox2.Items
                al.Add(li.Value)
            Next
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2008 10:30 AM
  • User282693488 posted

    Thank you, it worked

    Sunday, November 9, 2008 9:29 PM