Can someone please explain why this works:
RibbonTab rt_selecteda = (RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem;
string a = rt_selecteda.Tag.ToString();
but this fails:
string ab = (RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem.Tag.ToString();
'object' does not contain a definition for 'Tag' and no extension method 'Tag' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) W:\Dev_Code\Reporting_App\Core\Ribbon_Common.cs 114 78 Reporting_App
This
string ab = (RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem.Tag.ToString();
is not the same as (note the brackets)
string ab = ((RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem).Tag.ToString();
which is the one-line equivalent to your first code
RibbonTab rt_selecteda = (RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem;
string a = rt_selecteda.Tag.ToString();
(RibbonTab)MainWindow.Ribbon_Main_Link.SelectedItem.Tag.ToString() means:
Take MainWindow.Ribbon_Main_Link.SelectedItem
Get its Tag property
Call the Tag's ToString method
Cast the result as an RibbonTab
This fails since a. MainWindow.Ribbon_Main_Link.SelectedItem is of type object and doesn't have a Tag property, and b. even if it had, ToString returns a string and you can't cast a string as a RibbonTab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With