1

I've been working on a project, where I need something like a Dictionary, but at the same time also a List(Of KeyValuePair(Of String, String)). I have two questions, one on which type to use and how to preform the tasks I need, two to get confirmation on the possibility of another requirement.

The project is based on RS232 communication, and I have all the serial port stuff sorted. I have a group of units, split into two types, that get placed in a random order. I need to find the units that are on and sort the order they're in. I have a list of addresses to the units that I need to communicate with, the addresses are stored as hexadecimal strings. For each address I have a status bit which is also stored as a string, as a 1 or 0.

I need to be able to iterate through my list of unit arrays (which are read in numerical order of the addresses from a text file) and sort them based on the order the units are physically in real life (in another List/Dictionary)

I need to be able to manipulate the List or Dictionary in a few ways.

  • Firstly, I need to be able to add and remove entries (which both can do)
  • Secondly, I need to be able to access the index of a desired key.
  • Thirdly, I need to be able to iterate through all the entries in a For Each loop.

The problems I've found is that with the Dictionary, they're unsorted so I can't find the index of a specific Key, and I've been getting the same problem with the List(Of KeyValuePair... where I try and Add to and Remove from the two arrays, but I can't get the index to extract the value of the state bit in the unsorted string.

The second question is about the fact I want to (in the next step) have a data type that has the above list/dictionary as well as an array of strings that represents a list of sensors on that unit and their states.

Thanks for any help!

Edit: The suggested duplicate gives answers for indexing by the key. However I need the integer index of the Dictionary or List to store in a separate variable. This is required to ensure the reply is the same address as the one asked for (as it broadcasts to all units). A bit more clarification of what I'm iterating over: I'm iterating over the decreasing array, as I need to check every unit still in that array for their status, and am removing the one that's just been added to the sorted array.

Edit 2: I have found a current workaround to the problem. I use a dictionary, and make an ICollection of it's keys and iterate through that. I'm now wondering whether having a sorted "list" in a regular dictionary is bad practice or not. I only need it to look up relative to the keys, but it is important that it is always in the same order. Should I then change it to a sorted list or will it always appear in the same order?

Edlothiad
  • 133
  • 1
  • 6
  • 1
    Possible duplicate of [Get Index for a keyvaluepar in dictionary c# based in the value](http://stackoverflow.com/questions/4538894/get-index-for-a-keyvaluepar-in-dictionary-c-sharp-based-in-the-value) – Visual Vincent Jul 12 '16 at 10:34
  • @BanForFun : It doesn't matter if his question isn't in C#, the same answer still applies (see the first paragraph of the accepted answer). Either way one could easily convert from C# to VB.NET via an [**online converter**](http://converter.telerik.com/) – Visual Vincent Jul 12 '16 at 14:04
  • ... Almost. And Online converters don't always convert the code correctly. – BanForFun Jul 12 '16 at 14:06
  • @BanForFun : Not always, but mostly. If not, you try to solve it yourself or you ask a question here. :) – Visual Vincent Jul 12 '16 at 14:09
  • @VisualVincent The accepted answer describes using the Keys as the indexing. It is however of utmost importance for me that the order of the entries in the Dictionary (which my edit shows is what I managed to work around with) remain the same. Does this mean I should be using something other than a dictionary, as the accepted answer says _"you'll get the items in some order, but that order isn't guaranteed and can change over time"_ – Edlothiad Jul 12 '16 at 14:17
  • You should probably use something else, yes. Perhaps an [OrderedDictionary](https://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary(v=vs.110).aspx) will do the trick? – Visual Vincent Jul 12 '16 at 14:25
  • That seems to be on the right track, however is there a way to find the entry at index "I" of an Ordered Dictionary, furthermore, is there a way to see what data has been stored in the OrderedDictionary, I can only see a list of Properties, not the actual data stored in the OrderedDictionary – Edlothiad Jul 12 '16 at 15:14
  • I just did some more research and found I am able to find the entry at index "I", now the only thing I'd require is to know how to look at the Data stored in the Dictionary when I debug. – Edlothiad Jul 12 '16 at 15:16

0 Answers0