Reputation: 10287
A funny thing happened on the way to my first compilation of the week. I came across this line of code:
if (SetFetchTab)
tabMain.SelectedIndex = 1;
...and decided, well, I'll make that into a const to make it more readable, and enclose it in braces while I'm at it, in case additional code needs to be added to this condition later:
const int FETCH_TAB = 0;
const int CONNECTION_TAB = 1;
. . .
if (SetFetchTab)
{
tabMain.SelectedIndex = CONNECTION_TAB;
}
But then curiosity got the best of me, and I decided to find out where SetFetchTab is assigned a value...it's not -- except implicitly assigned false/0 where it's declared:
public static bool SetFetchTab;
At one time, another form conditionally set SetFetchTab to either 0 or 1, but that code is now commented out. SO, the condition above will NEVER be true, and the SelectedIndex will never be assigned CONNECTION_TAB/1. Therefore, why is this block not grayified, signifying it is dead code?
BTW, FETCH_TAB is grayified/recognized as a dead declaration, as tabMain.SelectedIndex is never assigned 0, and thus I had nowhere to use this.
Upvotes: 2
Views: 244
Reputation: 10287
Ah, I see that Resharper is smarter than me again (that's why I pay it the big bucks). I found this code:
if (!saveThisUser_Validate(ref txtSiteNbr, 0)) return false;
if (!saveThisUser_Validate(ref txtIP, 1)) return false;
private bool saveThisUser_Validate(ref TextBox tb, int index)
{
if (tb.Text != "")
return true;
MessageBox.Show("Not all required fields have been filled in", CCR.GetFormTitle("", "", ""));
tb.Focus();
tabMain.SelectedIndex = index;
return false;
}
...and have changed it to:
if (!saveThisUser_Validate(ref txtSiteNbr, FETCH_TAB)) return false;
if (!saveThisUser_Validate(ref txtIP, CONNECTION_TAB)) return false;
Upvotes: 0
Reputation: 8562
R# is playing it safe. It's not assuming all assemblies that are part of your application are in your solution. Since its public, another assembly, possibly one that gets dynamically loaded, could set the public field back to zero. If you were to remove it, you'd break that hypothetical assembly. If you're sure it is only used internally, set it to internal or private, and R# should detect that its not used.
Upvotes: 4
Reputation: 12654
Since SetFetchTab is a public field, and not a variable, it can be changed by external code - directly from yet unknown library or via reflection.
If it was a variable in a method, then it could not be changed by any external code, so Resharper could conclude that code is dead.
Upvotes: 6
Reputation: 62276
Well, the variable is assigned public static bool SetFetchTab;
to its default value, due the declaration, so this is equal to write like
public static bool SetFetchTab = default(bool);
and it is used
if (SetFetchTab)
{
tabMain.SelectedIndex = CONNECTION_TAB;
}
according to the code provided, so there is no any issue here.
The fact that the value will never changed, I don't think that anyone is able to identify with relevant percentage of success,at least not that I'm aware of. Consider that the value of SetFetchTab
can be changed from anywhere (it's a public static
) also using a reflection.
Upvotes: 4