I have a Generic List that I populated in one class, I want to return this List to the main form. Is this possible? In other words I want to pass a Generic List from one class to another, how would I do this?

Dani AI

Generated

Yes — you can pass or return a Generic List between classes. , the usual patterns are: build the List(Of T) in your helper class and return it from a Function, expose it as a Property, or accept/modify it as a parameter. is correct to clarify: List(Of T) holds typed objects. Note: List(Of T) is a reference type — passing it ByVal passes the reference (changes to the contents are visible to the caller); to replace the caller's reference either return a new list or pass the parameter ByRef.

Example patterns in VB.NET:

Public Class Provider
Public Function GetItems() As List(Of MyType)
Dim items As New List(Of MyType)
' populate items here
Return items
End Function
End Class

' consumer:
Dim provider As New Provider()
Dim items As List(Of MyType) = provider.GetItems()

To hide implementation or prevent modification, return an interface or read-only wrapper (for example IEnumerable(Of T), IReadOnlyList(Of T) or myList.AsReadOnly()).

Cautions: List(Of T) is not thread-safe — protect it with locks or use concurrent collections for multi-threaded access. For reference documentation see the List(Of T) class and the AsReadOnly method.

Member Avatar for Member #46692

As in a list of objects?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.