我正在尝试创建一个本质上返回具有以下结构的数组的函数: (String,String,Integer)
起初,我使用的是SortedList(Of String,SortedList(Of String,Integer)),但它似乎是一个巨大的浪费,因为我不需要数据后者的数据。现在我想的是矩形数组或DataTable,但我不知道如何定义矩形数组以使用多种类型,我不知道DataTable在运行时的速度有多快。
该函数将作为类函数频繁使用,因此更快,更好。 以下是手头代码的片段:
Public Function GetDirectoryUsagePktTool(ByVal EIAFilePath As String, Optional ByVal PocketCount As Integer = 120) As SortedList(Of String, SortedList(Of String, Integer))
'Tracks the usage of all tools currently loaded in the machine against all tools in a specified directory
' ordered from least used to most used.
If Not IO.File.Exists(EIAFilePath) Then
'Handle invalid filepath
Throw New ApplicationException("{ToolStatus}GetDirectoryUsagePktTool: EIAFilePath doesn't exist")
End If
Dim EIADirectory As String = EIAFilePath.Remove(EIAFilePath.LastIndexOf("\"))
'Get all Tool ID's with all of their FilePaths and Usage Counts per File
Dim dtl As SortedList(Of String, SortedList(Of String, Integer)) = GetDirectoryToolList(EIADirectory, EIAFilePath.Remove(0, EIAFilePath.LastIndexOf("\") + 1))
'Get all Tool ID's currently loaded in each (associated with) Pocket ID
Dim pl As SortedList(Of String, String) = GetPocketList(PocketCount)
'Return object
Dim dupl As New SortedList(Of String, SortedList(Of String, Integer))
'Counter for Total Usage count
Dim cntr As Integer
'Enumerate through each Pocket ID
For Each Pocket As String In pl.Keys
'Reset Total Usage count
cntr = 0
'Verify existence of Tool ID in directory search
If Not IsNothing(dtl(Pocket)) Then
'Enumerate through File Paths using Pocket's Tool ID
For Each FilePath As String In dtl(pl(Pocket)).Keys
'Count Tool ID Usage
cntr += dtl(pl(Pocket))(FilePath)
Next
'Return object's Tool ID and Usage values
Dim tu As New SortedList(Of String, Integer)
'Probably not necessary, but ensure no overwriting or appended usage to Return Object
If IsNothing(dupl(Pocket)) Then
'Add reference of Pocket No. to Tool Group No and Total Count
' (Pocket ID, (Tool ID, Usage))
tu.Add(pl(Pocket), cntr)
dupl.Add(Pocket, tu)
End If
End If
Next
Return dupl
End Function
编辑:忘记提及由于操作系统限制,代码必须与.NET 2.0及更早版本兼容。
解决方案: 正如所建议的那样,我创建了一个新的Structure并将其作为数组返回,因为我需要Pocket ID的所有引用,以便随后的工具ID和用法。新结构如下:
Structure PocketTools
Public PocketID As String
Public ToolGroupID As String
Public UsageCount As Integer
End Structure
返回对象的声明:
'Return object
'Dim dupl As New SortedList(Of String, SortedList(Of String, Integer))
Dim dupl() As PocketTools
添加到返回对象
'Add reference of Pocket No. to Tool Group No and Total Count
' (Pocket ID, (Tool ID, Usage))
If IsNothing(dupl) Then
ReDim dupl(0)
Else
ReDim Preserve dupl(dupl.Length)
End If
dupl(dupl.Length - 1).PocketID = Pocket
dupl(dupl.Length - 1).ToolGroupID = pl(Pocket)
dupl(dupl.Length - 1).UsageCount = cntr
'tu.Add(pl(Pocket), cntr)
'dupl.Add(Pocket, tu)
答案 0 :(得分:5)
我建议创建一个结构或类来处理函数的返回类型。例如:
Structure MyStruct
Public Value1 As String
Public Value2 As String
Public Value3 As Integer
End Structure
听起来你根本不需要一个数组,如果它总是两个字符串和一个int。
答案 1 :(得分:0)
您不能将多维度数组用于不同的数据类型,但您可以使用Tuple
的数组(或列表)。
在这种情况下,在我看来,三参数元组的数组似乎是一个明显的选择。