我正在寻找一种避免使用select大小写访问特定列表的方法;我将在一个模块中拥有大约90个列表,并且根据列表框中选择的记录(手动填充数据库中大多数表的名称,但不是所有这些表的名称),我需要读取列表中的项。所以我有这样的东西:
Public RelevantTables_Table001 As List(Of Table001) = New List(Of Table001)
Public RelevantTables_Table002 As List(Of Table002) = New List(Of Table002)
'...
Public RelevantTables_Table999 As List(Of Table999) = New List(Of Table999)
Class Table001
'code for populating RelevantTables_Table001
End Class
Class Table002
'code for populating RelevantTables_Table002
End Class
Class Table999
'code for populating RelevantTables_Table999
End Class现在我需要阅读相关的列表,这取决于列表框中选择的项目。例如,如果有人选择Table042,我需要阅读列表RelevantTables_Table042的条目。
我正在尝试使用DirectCast来实现这个目标,但我不知道如何实现它。
发布于 2014-10-30 18:46:05
类:
Public Class Table
Public Tablename As String
Public Collection As New List(Of String)
Public Overrides Function ToString() As String
Return Me.TableName
End Function
End Class创建新列表:
Private RelevantTable_Table001 As New Table
RelevantTable_Table001.Tablename = "Table001"
RelevantTable_Table001.Collection.Add("stuff")
...
'add the class and it will display the TableName since we
'overrided the ToString function
lsb.Items.Add(RelevantTable_Table001)
'class objects can be stored in the listbox as an object从SelectedItem属性获取List对象。
Private Sub lsb_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim tableList = TryCast(DirectCast(sender, ListBox).SelectedItem, Table)
If tableList IsNot Nothing Then
'tableList is the reference to the table object you seek.
End If
End Sub若要列出多个对象(DGV中的列),请使用自定义类:
Public Class MyCustomClass
Public Property Prop1 As String
Public Property Prop2 As String
Public Property Prop3 As String
End Class然后您的Table.Collection将是一个List(Of MyCustomClass)而不是一个字符串,这将为每个集合项提供3个项--这是一个表。这符合你的需要吗?
发布于 2014-10-30 18:27:21
列出列表,然后使用下标访问正确的列表,如
Public RelevantTables As List(Of List(Of table))
For Each item in RelevantTables(42)https://stackoverflow.com/questions/26659338
复制相似问题