1

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)
1
  • 1
    Some notes about your chosen solution. You don't want to expose fields publicly, it's best to define those as properties. You also should favor a class over a structure here since it is apparently mutable and doesn't define a collective value. Commented Jun 8, 2015 at 18:42

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

Sorry, forgot to mention that code needs be compatible with .NET 2.0 and earlier. Otherwise, yes this would be probably the easiest (maybe fastest?) method. I'll edit question to relay.
In that case use a struct.
A tuple isn't terrible, though I wouldn't call it an obvious choice. The obvious choice is to define a well named class with meaningful properties. The tuple is not quite as ideal, and is certainly not something I would let escape from a method (an an input or as a return type, for example).
@anthony: it's a simple and quick solution. But I ended up voting for the other answer as it is better...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.