2

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'.

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Public Shared Function FillList() as ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.