T
Tom P.
I'm not getting the theory behind LINQ, and without that I can't get
my head around how to use it and why.
Here's what I _think_ I'm trying to do:
I've got a menu with several items. Some other part of the application
is changing the text in a combobox and I want to set the indicator to
the correct menuitem. I can loop through the Items collection and look
at the Tag for my indicator, like so:
foreach (ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items)
{
if (CurrentButton.Text == "")
continue;
string ItemPath = (string)CurrentButton.Tag;
if (ItemPath == FavoritePath)
{
base.Text = CurrentButton.Text;
break;
}
}
But I think using LINQ would be a better solution.
My first question is, would using LINQ actually be a better solution?
Isn't LINQ just going to loop like I would?
My second question is why doesn't this code work?
ToolStripItem CurrentItem;
var favoriteItem =
from ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items
where ((string)Tag) == FavoritePath
select CurrentButton;
CurrentItem = (ToolStripItem)favoriteItem;
base.Text = CurrentItem.Text;
More to the point, why doesn't the LINQ part work and what's wrong
with it?
After several attempts to get it to compile I finally ran it and got
the following error:
" Unable to cast object of type '<WhereIterator>d__0`
1[System.Windows.Forms.ToolStripItem]' to type
'System.Windows.Forms.ToolStripItem' "
Why can't it cast something to the same type? Why is it casting
anything? What am I doing wrong? I need to understand this stuff but
these errors are not helping.
Thanks for the help... and the patience.
Tom P.
my head around how to use it and why.
Here's what I _think_ I'm trying to do:
I've got a menu with several items. Some other part of the application
is changing the text in a combobox and I want to set the indicator to
the correct menuitem. I can loop through the Items collection and look
at the Tag for my indicator, like so:
foreach (ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items)
{
if (CurrentButton.Text == "")
continue;
string ItemPath = (string)CurrentButton.Tag;
if (ItemPath == FavoritePath)
{
base.Text = CurrentButton.Text;
break;
}
}
But I think using LINQ would be a better solution.
My first question is, would using LINQ actually be a better solution?
Isn't LINQ just going to loop like I would?
My second question is why doesn't this code work?
ToolStripItem CurrentItem;
var favoriteItem =
from ToolStripItem CurrentButton in
_contextMenuStripFavorites.Items
where ((string)Tag) == FavoritePath
select CurrentButton;
CurrentItem = (ToolStripItem)favoriteItem;
base.Text = CurrentItem.Text;
More to the point, why doesn't the LINQ part work and what's wrong
with it?
After several attempts to get it to compile I finally ran it and got
the following error:
" Unable to cast object of type '<WhereIterator>d__0`
1[System.Windows.Forms.ToolStripItem]' to type
'System.Windows.Forms.ToolStripItem' "
Why can't it cast something to the same type? Why is it casting
anything? What am I doing wrong? I need to understand this stuff but
these errors are not helping.
Thanks for the help... and the patience.
Tom P.