Reputation: 1305
Why do I get the following warning in my C# code?
the private field xxxx is assigned but its value is never used
The attribute in question is only used as a flag for the user. Indeed the user can access (get) the value false
or true
to know a state of something.
Here is an example of code which creates this warning:
class Class1 {
// Attributes
private b_myboolean = false;
// Accessors
public bool B_myboolean
{
get{ return b_myboolean;}
}
// Methods
// Somewhere in a method
b_myboolean =true;
}
Something like that will return the warning. Why? Do I have to code this flag another way?
Upvotes: 0
Views: 45607
Reputation: 1133
I am using Visual Studio 2015 Express.
In my case, everything in my code is correct, but I get the same warning!
I closed my Visual Studio solution, and reopened it. It worked.
It has happened many times!
I guess the issue is because of a Visual Studio bug!
Upvotes: 0
Reputation: 93
For me, I was getting the same warning and when I looked in my code I saw for sure it was being used.
So I had a private field like this:
private bool isClosing;
I even set it to false in the constructor. And then on the MouseClick event I had it set to True. So how the heck was it not being used? And then it dawned on me!!!
You see, just setting a variable to a value at various places in my code is not using it. It's just setting it to various values. Unless I'm using it in some logic (if
statement) it is not really being used.
You have to think about that for a moment...and if you really want to know for sure...go ahead and delete all the places where you set it to some value...you'll find your code will still work just as it did before the deletes.
It has to because you never used the variable for anything. Again, setting a variable to different values throughout the code is not using it!!
Now that is clever Visual Studio. :)
Upvotes: 7
Reputation: 947
You can disable this specific warning.
Here are two candidates causing warnings with fields not used or its value never used:
Warning 169: The field 'xxx' is never used Warning 414: The field 'xxx' is assigned but its value is never used
#pragma warning disable 414
// Attributes
private b_myboolean = false;
#pragma warning restore 414
I also ran into warning messages. In general one should of course not disable warnings. But surrounding just this field is not too dramatic.
Upvotes: 14
Reputation: 1502406
The code you've posted won't compile at all, let alone show the right warning... but I'm going to guess that your actual code looks like this:
private bool b_myboolean;
public bool B_myboolean
{
get { return B_myboolean; } // Bug!
}
Now this is never using the field - because the property will call itself recursively.
Only a guess, of course - if you update your code to something more realistic, we can say for sure.
Upvotes: 11
Reputation: 539
This warning is only to show you that it is never explicitly used in your code.
Many times you will get this warning in try-catch statements, or other conditional statements.
It is nothing to be worried about, just a reminder to look through your code to ensure you aren't wasting a declaration.
Upvotes: 1