This question pulls content and ideas from a good many of my other questions and their answers, and pulls a lot of things together. I am quite satisfied with my approach here, but seeing as I'm refactoring my app, I might as well ask about my practices and get suggestions for improvements.
This is my search method (thank you for helping me with it, Heslacher):
private static void GetResults(ref string[] query, ref ObservableCollection<int> weight)
{
int position = -1;
foreach (string[] array in SearchKeys.Keys)
{
position++;
int length = array.Length;
int middle = length / 2;
char firstCharMidArray = array[middle][0];
foreach (string s in query)
{
if (s.Length == 0) continue;
int min = array[middle][0] < s[0] ? middle : 0;
int max = array[middle][0] <= s[0] ? array.Length : middle + 1;
for (int i = min; i < max; i++)
{
weight.Add(0);
if (array[i] == s)
{
if (weight[position] == 0)
{
Data.SearchList.Add(SearchKeys.Pages[position]);
}
weight[position]++;
}
}
}
}
}
It works by pulling data from this:
public static ResourceLoader resourceFile = new ResourceLoader();
public static string[][] Keys = { resourceFile.GetString("SearchWSOneNote").Split(' '),
resourceFile.GetString("SearchWSMainMenu").Split(' '),
resourceFile.GetString("SearchWSTextMenu").Split(' '),
resourceFile.GetString("SearchWSTextBlockMenu").Split(' '),
resourceFile.GetString("SearchWSTableMenu").Split(' '),
resourceFile.GetString("SearchWSTableCellsMenu").Split(' '),
resourceFile.GetString("SearchWSDrawMenu").Split(' '),
resourceFile.GetString("SearchWSDrawnItemsMenu").Split(' '),
resourceFile.GetString("SearchWSPictureMenu").Split(' '),
resourceFile.GetString("SearchWSFileMenu").Split(' '),
resourceFile.GetString("SearchWSAppBarsMenu").Split(' '),
resourceFile.GetString("SearchWSCharmsMenu").Split(' '),
resourceFile.GetString("SearchWSKeyboardShortcuts").Split(' '),
resourceFile.GetString("SearchWSColorPalate").Split(' ')
/* more here... */ };
public static MenuItem[] Pages = { new MenuItem(resourceFile.GetString("OneNote").TrimStart(' '), typeof(WindowsData.OneNote), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSMainMenu").TrimStart(' '), typeof(WindowsData.MainMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSTextMenu").TrimStart(' '), typeof(WindowsData.TextMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSTextBlockMenu").TrimStart(' '), typeof(WindowsData.TextBlockMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSTableMenu").TrimStart(' '), typeof(WindowsData.TableMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSTableCellsMenu").TrimStart(' '), typeof(WindowsData.TableCellsMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSDrawMenu").TrimStart(' '), typeof(WindowsData.DrawMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSDrawnItemsMenu").TrimStart(' '), typeof(WindowsData.DrawnItemsMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSPictureMenu").TrimStart(' '), typeof(WindowsData.PictureMenu), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSFileMenu").TrimStart(' '), typeof(WindowsData.File), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSAppBarsMenu").TrimStart(' '), typeof(WindowsData.AppBar), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSCharmsMenu").TrimStart(' '), typeof(WindowsData.Charms), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSKeyboardShortcutsMenu").TrimStart(' '), typeof(WindowsData.KeyboardShortcuts), Menus.SearchResult),
new MenuItem(resourceFile.GetString("WSColorPalateMenu").TrimStart(' '), typeof(WindowsData.ColorPalate), Menus.SearchResult)
/* more here... */ };
If you are not sure about any of the other code is called, and how it interacts with this, please feel free to ask for it or an explanation of what it does. This works because each index of the collection of search values in Keys
corresponds to the index of the matching MenuItem
in Pages
. My keys are all stored in a .resw (resources) file for ease of adding multiple languages. I have a feeling this isn't good, although it is simple enough to maintain, so please tell me everything wrong with it.
weight
or adding toData.SearchList
? \$\endgroup\$resourceFile.GetString("SearchWSOneNote").Split(' ')
and a dozen of these:new MenuItem(resourceFile.GetString("OneNote").TrimStart(' '), typeof(WindowsData.OneNote), Menus.SearchResult)
: why don't you create methods to avoid copy-pasting these lines over and over again and merely changing one or two things? That would significantly improve the readability of that code. \$\endgroup\$Data.SearchList
. \$\endgroup\$