Reputation: 1267
The IEnumerable in question is a list of an object containing filenames and properties relating to those file names, and the string is a directory name, such as "C:\Test\Testing". My goal is to store these two pieces of data together so that they're 'linked', in a sense, which should make them easier to use together, as the IEnumerable will become the source for a DataGrid, and the string the text of a label stating the current directory.
How would I go about achieving this? I initially thought of a dictionary, but that doesn't seem to work here. I need to be able to grab the 'top-most' item, so to speak, whenever a button is pressed, and dictionaries are, I believe, unordered.
Upvotes: 0
Views: 950
Reputation: 7919
While the accepted answer works, I would recommend creating a class for this, rather than using a KeyValuePair
:
public class FilesInDirectory
{
public string Directory { get; set; }
public IEnumerable<string> FileNames { get; set; }
}
In OOP you should strive to avoid primitive obsession. A KeyValuePair
is a kind of primitive, as it doesn't convey any meaning and can't be extended.
item.Key
and then iterating over item.Value
, things will get confusing - far less so than item.Directory
and item.FileNames
KeyValuePair
, you will have to define that method on some other class, whereas it actually fits right at home on FilesInDirectory.GetFiles()
Upvotes: 2
Reputation: 2696
How about a List<KeyValuePair<IEnumerable, string>>
?
var list = new List<KeyValuePair<IEnumerable, string>>();
list.Add(new KeyValuePair<IEnumerable, string>(data, path));
If that sounds too awkward for you, feel free to use a custom class instead.
Feel free to use a Queue<>
, Stack<>
or anything else that fits your needs, KeyValuePair
can be used with any collection type.
Upvotes: 2