dinotom dinotom - 4 months ago 10
Vb.net Question

how to set constructor with List(of Class) parameter

Given the scenario where I am building a wrapper for the data I pass to jquery datatables and I have for example, three Classes Orders, OrderDetails and Customers. In datatables, lists of objects are denoted by the variable name aoData, so to construct that I have to pass a List(Of someclass). Since I don't know which class until I pass it how do you do it? is There a GetType that takes the class name as a string? I couldn't find one.

Public Class DataTablesWrapper
Public Sub New(ByRef data As List(Of String))
Me.aaData = data
End Sub
Public Sub New(ByRef data As List(Of some class depending on what data I want wrapped))
Me.aoData = data
End Sub
Public Property aaData As List(Of String)
Public Property aoData As List(Of some class depending on what data I want wrapped)
End Class

Answer

I would recommend the use of generics.

In your case it would look like this:

Public Class DataTablesWrapper(Of T)
    Public Sub New(ByRef data As List(Of T))
        Me.aaData = data
    End Sub

    Public Property aaData As List(Of T)
End Class

Creating your instance would then look like this:

Sub Main()
    Dim stringList As New List(Of String)({"foo", "bar"})      
    Dim tablesWrapper As New DataTablesWrapper(Of String)(stringList)
End Sub

You don't want to use List(Of Object) due to this reasons.

Comments