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; public string Name; } 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(); } } 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; } }
source share