Home List of Dictionary to multidimensional array
Reply: 2

List of Dictionary to multidimensional array

treeNinja
1#
treeNinja Published in 2018-01-12 18:32:29Z

I am trying to pass a list of files and paths to a third party method that accepts only a String(,). this selection can vary depending on the users selections.

I thought the following would represent then 2D array, holding the path and the name of the files.

myFiles As New List(Of Dictionary(Of String, String))()

But when I have to pass this to the method for example

ProcessFiles(ByVal Attachments(,) As String)

With

 ProcessFiles(myFiles.ToArray())

I get the error

"Value of 'Dictionary(Of String, string)() cannot be converted to a String(,) because the array types have different numbers of dimensions.

How do I define my List to be representative of the array?

The array is expecting a layout of

(0,0) --> "\\location\Of\File"
(0,1) --> "filename"
(1,0) --> "\\location\Of\File2"
(1,1) --> "filename2"
garthhh
2#
garthhh Reply to 2018-01-12 20:30:13Z

Here's a relatively simple solution (which is essentially what Plutonix suggested) assuming each dictionary only has 1 value and that the dictionary key is the path and value is the name:

    ' temporary dictionary for loop iteration
    Dim currentDict As Dictionary(Of String, String)
    ' assuming each dictionary only has 1 path/name entry, set up 2D string array
    Dim myFileArray(,) As String = New String(myFiles.Count - 1, 1) {}

    ' assuming the dictionary key is the path and the dictionary value is the name, 
    ' iterate through myFiles and extract key/value into 2D array
    For i = 0 To myFiles.Count - 1
        currentDict = myFiles(i)
        myFileArray(i, 0) = currentDict.Keys(0)
        myFileArray(i, 1) = currentDict.Values(1)
    Next

    ProcessFiles(myFileArray)

I played around with some LINQ queries, but .ToArray yields jagged arrays (which are not the same as multidimensional arrays), so it might not be possible/practical to go that route if you absolutely need a 2D array.

djv
3#
djv Reply to 2018-01-12 22:15:01Z

Accepted answer is in general a way to handle it.

However, if you have duplicate directories (i.e. two files in the same directory), you can't use a directory as a key to a dictionary, because the key must be unique.

A List(Of ... KeyValuePair, struct, Tuple, custom class, or existing class ) would get around the collision.

List(Of KeyValuePair) is close to Dictionary in that they expose KeyValuePair when Enumerated, and you could simply replace Dictionary(Of String, String) in garthhh's answer with List(Of KeyValuePair(String, String)) to avoid collisions.

Specifically, I think System.IO.FileInfo might be a class for your case, since you are working with files. So, using a List(Of System.IO.FileInfo) ...

Dim myFileListFileInfo As New List(Of System.IO.FileInfo) From {
    New FileInfo(System.IO.Path.Combine("\\location\Of\File", "filename")),
    New FileInfo(System.IO.Path.Combine("\\location\Of\File2", "filename2")),
    New FileInfo(System.IO.Path.Combine("\\location\Of\File3", "filename3"))
} ' initialize this way...
' ... or add like this
myFileListFileInfo.Add(New FileInfo(System.IO.Path.Combine("\\location\Of\File4", "filename4")))
Dim myFileArray(myFileListFileInfo.Count - 1, 1) As String
For i = 0 To myFileListFileInfo.Count - 1
    myFileArray(i, 0) = myFileListFileInfo(i).DirectoryName
    myFileArray(i, 1) = myFileListFileInfo(i).Name
Next

Or maybe it's overkill if you really just want an array(,). As I said, you could use a number of things in place of FileInfo from classes, Tuples, etc.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO