SortedDict n' more confusion

Phix

Member
Joined
Apr 9, 2010
Messages
15
Location
San Jose
Programming Experience
3-5
For getting used to working in vb.net, I'm making a small project management app for myself to set up project directories when I start a new job. I have an XML config file that has the project directories and file names to be created "by default". For each file name I have 3 controls - a checkbox for deletion, a textbox for the filename, and a combobox to set the directory it's created in when I decide all parameters are in order.

So, not having too much experience in vb, I decided out of all the collections a SortedDictionary(Of String, Of List(Of Control)) would be the best way. That way, when I need to add or remove a file from the panel displaying that info, I would just need to alter the dictionary and repopulate the panel on the form, and I'd need a sorting to keep my groupings in order.

For example:

VB.NET:
Key: Group1
     Values: CheckBox, TextBox, ComboBox
Key: Group2
     Values: CheckBox, TextBox, ComboBox
Key: Group3
     Values: CheckBox, TextBox, ComboBox

etc..

Everything works great, actually, except once I hit 10 or more files in that collection, the sorting doesn't work as I'd hope.

When I output the FilesDict.Keys.Max value, I get something like the following after a few clicks of the add button:
VB.NET:
...
Group7
Group8
Group9
Group9
Group9

The > 10 values are there, but they're stuck ahead of 1. From a computer standpoint this makes perfect sense, but I need to retrieve the absolute max key from the dictionary to prevent duplicate keys. As of now, I'm taking the Count of the FilesList to get the new group number, but once I delete some values, that no longer works and I get some duplicate keys.

I've tinkered with LINQ and some rather messy functions, but I was hoping there'd be something I was missing. Hopefully I laid this out flat enough, little tough to explain.
 
First up, you should look at creating a UserControl. You add a UserControl to your project just as you add a Form. You design a UserControl just as you design a form. When it comes time to use a UserControl, you use it just like any other Control. This allows you to create a single control that groups together your CheckBox, TextBox and ComboBox and use them as a unit. You can then use a SortedDictionary(Of String, MyUserControl).

As for your sorting issue, the constructor is overloaded and allows you to specify an IComparer to perform custom sorting. You can define your own class that implements the IComparer(Of String) interface and compares two strings by removing the alpha part, converting the numeric part to a number and then comparing those two numbers. You then pass an instance of that class to the SortedDictionary constructor and it will be used to sort your groups.
 
I did find some stuff for the IComparer interface, and thought I would have to dig into that, so thank your for confirming... and thank you a TON for that userControl suggestion. Makes life so much easier, and the code a lot less dense.

Cheers :)
 
Back
Top