问题
I'm posting this message because I'm wondering about a warning I got in my c#
code:
the private field xxxx is assigned but its value is never used
The attribute in question is only use 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 create this warning :
class Class1 {
//Attributs
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. Do you know why ? Do I have to code this flag another way ?
Thank you for your help by advance.
Joze
回答1:
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.
回答2:
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
//Attributs
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.
回答3:
I know I'm contributing to this question almost three years after it was originally asked but I since I got the same I thought I'd just throw something else out there.
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. Its 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. :)
Hope that helps...and I may be wrong...but thought I'd give you some food for thought in any case!!
回答4:
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.
回答5:
I am using visual studio 2015 community edition.
In my case, everything in my code is correct, but I get the same warning!
Then I closed my visual studio solution, and re-open it, it works.
It happens many times!
I guess the issue is because Visual Studio's bug!
来源:https://stackoverflow.com/questions/6330491/weird-warning-is-assigned-but-its-value-is-never-used