views:

34

answers:

2

Hi guys, I learnt example from msdn to populate a listbox control with arraylist. http://msdn.microsoft.com/en-us/library/1818w7we(v=VS.100).aspx

I want to create a function which will give return the USStates arraylist and use the returned value as datasource for listbox1

    Dim USStates As New ArrayList()
    USStates.Add(New USState("Alabama", "AL"))
    USStates.Add(New USState("Washington", "WA"))
    USStates.Add(New USState("West Virginia", "WV"))
    USStates.Add(New USState("Wisconsin", "WI"))
    USStates.Add(New USState("Wyoming", "WY"))
    ListBox1.DataSource = USStates

    ListBox1.DisplayMember = "LongName"
    ListBox1.ValueMember = "ShortName

I tried creating a function like:

Public Shared Function FillList() As ArrayList()
    Dim USStates As New ArrayList()
    USStates.Add(New USState("Alabama", "AL"))
    USStates.Add(New USState("Washington", "WA"))
    USStates.Add(New USState("West Virginia", "WV"))
    USStates.Add(New USState("Wisconsin", "WI"))
    USStates.Add(New USState("Wyoming", "WY"))
    return usstates
end function

but it says error: Value of type 'System.Collections.ArrayList' cannot be converted to '1-dimensional array of System.Collections.ArrayList'.

+1  A: 

Make sure the return type of the function is correct (simply ArrayList, not ArrayList(). The first means you are returning an ArrayList, the second that you are returning an array of ArrayList:

Public Shared Function FillList() As ArrayList
    Dim USStates As New ArrayList()
    USStates.Add(New USState("Alabama", "AL"))
    USStates.Add(New USState("Washington", "WA"))
    USStates.Add(New USState("West Virginia", "WV"))
    USStates.Add(New USState("Wisconsin", "WI"))
    USStates.Add(New USState("Wyoming", "WY"))
    return usstates
end function
Oded
thnx oded it was good explanation from you. :)
KoolKabin
A: 

You need to remove the parenthesis from the end of your function definition.

Public Shared Function FillList() as ArrayList
Walter