.NET Array with Multiple Data Types

6k views Asked by At

I'm trying to create a function that essentially returns an array with the following structure: (String, String, Integer)

At first, I was using a SortedList(Of String, SortedList(Of String,Integer)), but it seems like a huge waste since I don't need an array for the latter of the data. Now I'm thinking either a Rectangular Array or a DataTable, but I don't know how to define the Rectangular Array to use multiple types and I don't know how fast the DataTable will be at runtime.

The function will be used quite frequently as a class function, so the quicker, the better. Here's a snippet of the code at hand:



    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

EDIT: Forgot to mention that code must be compatible with .NET 2.0 and earlier due to OS constraints.

Solution: As suggested, I created a new Structure and returned it as an array since I need all references of the Pocket ID to it's subsequent Tool ID and usage. The new structure as follows:


    Structure PocketTools
        Public PocketID As String
        Public ToolGroupID As String
        Public UsageCount As Integer
    End Structure

The declaration of the return object:


        'Return object
        'Dim dupl As New SortedList(Of String, SortedList(Of String, Integer))
        Dim dupl() As PocketTools

The addition to the return object


                    '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)
2

There are 2 answers

0
AngularRat On BEST ANSWER

I'd suggest creating a struct or class to handle the return type of the function. For example:

Structure MyStruct
    Public Value1 As String
    Public Value2 As String
    Public Value3 As Integer
End Structure

It sounds like you really don't need an array at all, if it's always two strings and an int.

4
Zohar Peled On

You can't use a multiple dimensionl array for different data types, but you can use an array (or list) of a Tuple.

An array of a three parameter Tuple seems to me like an obvious choice in this case.