Wrong Resharper clause - unused variable (actually used when binding data)

I have a piece of code that Resharper tells me that it has unused variables, but the variables are definitely used. Variables are used in Databind (), and fields for binding are set as strings. Because field names are accessible using a string variable, Resharper does not consider them to be used.

In the following code example, Resharper tells me to change the public variable to private. After that, he tells me that the variable is not used and can be deleted. Both of these sentences are erroneous because the variable is used and should be publicly available.

I do not like that Resharper warns me about this and is yellow. I want to check my code in green. I know that I can ignore this using the option to suppress the comment, but in the past I never had to use this option, and I managed to find other solutions to get the green code. In this case, I could not find another way. Does anyone know how I can make Resharper recognize that this variable is being used?

using System; using System.Collections; using System.Web.UI.WebControls; public partial class TestCode_General_ResharperTest : System.Web.UI.Page { private class TestClass { public TestClass(string name, string id) { ID = id; Name = name; } public string ID; /*Resharper says this can be made private*/ public string Name; /*Resharper says this can be made private*/ } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DropDownList testList = new DropDownList(); ArrayList groups = getTestList(); testList.DataSource = groups; testList.DataValueField = "ID"; testList.DataTextField = "Name"; testList.DataBind(); /* Databind causes the public variables to be accessed.*/ } } private static ArrayList getTestList() { ArrayList groupInfo = new ArrayList(); string[] pairs = new[] { "Test:1", "Test 2:2", "Test 3:3" }; foreach (string pair in pairs) { string[] values = pair.Split(new[] { ':' }); groupInfo.Add(new TestClass(values[0], values[1])); } return groupInfo; } } 
+4
source share
6 answers

I think you have 3 options:

  • Use an anonymous class, since you still don't use strong typing.
  • Suppress the warning with a comment and add a comment explaining why.
  • Add the ReSharper [UsedImplicitly] attribute and add a comment explaining why (not sure if this file works with fields).

Example with option 1:

 using System; using System.Collections; using System.Web.UI.WebControls; public partial class TestCode_General_ResharperTest : System.Web.UI.Page { protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Page.IsPostBack) return; var testList = new DropDownList(); testList.DataSource = GetTestListData(); testList.DataValueField = "ID"; testList.DataTextField = "Name"; testList.DataBind(); /* Databind causes the public variables to be accessed.*/ } private static IEnumerable<object> GetTestListData() { var groups = new List<object>(); var pairs = new[] { "Test:1", "Test 2:2", "Test 3:3" }; foreach (var pair in pairs) { var values = pair.Split(new[] { ':' }); groups.Add(new { ID = values[0], Name = values[1] }); } return groups; } } 
+5
source

Resharper cannot know that the DataBind method in DropDownList will need to access these fields / properties. If you want to remove the warning, you can make them properties, and then create your TestClass, as others have mentioned, or you can suppress them with a comment.

I see this day in my work. Developers, judging by making Resharper happy and getting OCD over small squiggly lines and warnings, when they need to understand that Resharper is there to make suggestions that they can view, and then choose to ignore if their code makes sense.

+1
source

Make them properties as below

 private class TestClass { public TestClass() { } public string ID{get;set;} public string Name{get;set;} } 

and use it like

  groupInfo.Add(new TestClass{ID=values[0], Name= values[1]}); 
0
source

Try to make them properties

 private class TestClass { public TestClass(string name, string id) { ID = id; Name = name; } public string ID { get; set; } public string Name { get; set; } } 
0
source

ReSharper cannot know that some fields are used only through reflection.

Consider creating properties instead of open fields.

0
source

I do not see where you are using the identifier or name anywhere in the class. Its appointment in the constructor is not taken into account, by the way, this is probably the reason for his complaint.

Resharper is Erring in the direction of caution, recommending that the variable be private, because you are not using it anywhere; Since you did not define any getters or setters, it is likely that you are using these variables for internal state tracking.

0
source

All Articles