patterncsharpMinor
Going back with a Vengeance
Viewed 0 times
withbackgoingvengeance
Problem
So I received a comment on my previous question that suggested creating a
So, here is my final code:
Everything else stayed the same as in my previous question, please check that for details.
Dictionary of my menus to store the data in. So that got me thinking, all I am doing is going back to the first instance of MenuItem in the ItemList that has a Menu of Back[0].Menu. So why not just iterate through there and open the menu when I find it?So, here is my final code:
public void GoBack()
{
NavButtonUsed = true;
Forward.Insert(0, Back[0]);
Back.RemoveAt(0);
if (Back[0].Title.StartsWith(" ") && CurrentItem.Menu != Back[0].Menu)
{
foreach(MenuItem mi in ItemList)
{
if (mi.Menu == Back[0].Menu)
{
CurrentItem = ItemList[ItemList.IndexOf(mi)];
break;
}
}
}
CurrentItem = ItemList[ItemList.IndexOf(Back[0])];
NavButtonUsed = false;
}
Everything else stayed the same as in my previous question, please check that for details.
Solution
CurrentItem = ItemList[ItemList.IndexOf(mi)];can just be
CurrentItem = mi;You could also use
FirstOrDefault:var item = ItemList.FirstOrDefault(i => i.Menu == Back[0].Menu);
if (item != null)
{
CurrentItem = item;
}Code Snippets
CurrentItem = mi;var item = ItemList.FirstOrDefault(i => i.Menu == Back[0].Menu);
if (item != null)
{
CurrentItem = item;
}Context
StackExchange Code Review Q#78937, answer score: 6
Revisions (0)
No revisions yet.