0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joze
  • 1,285
  • 4
  • 19
  • 33
  • 5
    Can you post the real code please? Your example isn't valid C# (your types aren't declared), and the warning may be being caused by something your example isn't showing. – The Evil Greebo Jun 13 '11 at 12:40
  • Your code is invalid ... you can't assign a field that way inside class. – Rahul Jun 13 '11 at 12:45
  • Ok, I tried it and I don't get the warning. –  Jun 13 '11 at 12:46
  • It's still not valid even with the edit. You need a "bool" in line 4 and you might as well put a function stub in around line 14. –  Jun 13 '11 at 13:10

5 Answers5

13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick
  • 907
  • 9
  • 22
11

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You are absolutely right... I just tried to write something which looks like my code. I'll correct it right now ! – Joze Jun 13 '11 at 12:56
7

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. :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amar Kapadia
  • 93
  • 1
  • 5
  • Nice point. IOW, 'Is not being used' in VS actually means 'is not being used for anything meaningful'. – WiredEarp Aug 16 '16 at 03:19
  • string sNotUsed = ""; //-- Somewhere else sNotUsed = sNotUsed.Trim(); //-- Will get rid of the warning although nothing useful is actually done. – Pradeep Puranik Dec 11 '19 at 14:47
1

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.

James
  • 539
  • 2
  • 6
  • 9
    Actually, I'd say it's almost *always* something to be worried about. If you're assigning a value and never using it, chances are you *think* you're using it, but you've screwed up somewhere. – Jon Skeet Jun 13 '11 at 12:44
  • 1
    @Jon: Of course, the compiler can't know what variables you like to view in the debug watch window, read via reflection. Not sure whether p/invoke scenarios might also trigger this warning spuriously. So *almost* always sounds accurate. – Ben Voigt Jun 13 '11 at 12:49
  • @Ben: Reading via reflection, absolutely - and I'd use a warning-suppressing pragma for that. Keeping state around solely for debugging sessions? Eek! – Jon Skeet Jun 13 '11 at 12:50
  • What do you think about something like that : #pragma warning disable 169, 414 private bool b_myboolean = false; #pragma warning restore 169, 414 Is it a good way to do this ? – Joze Jun 13 '11 at 12:59
  • @Jon: Tracking statistics, etc, solely for debugging is probably more common than we'd like to admit. I'm pretty sure the CLR JIT keeps a large amount of metadata just for debug purposes. Anyway, if I were doing this, I'd use `DebuggerDisplayAttribute` and a debug visualizer method, which incidentally would cure the warning. – Ben Voigt Jun 13 '11 at 12:59
  • 2
    @Joze: Without seeing your real code, we can't possibly know what you should be doing. – Jon Skeet Jun 13 '11 at 13:01
  • @Jon : My code do exactly the same thing as the example right now. "B_myboolean" is a "flag" the user of the class can use to check if there is a current access to something. I think using a method which check "b_myboolean" and return false/true is a better way to do that. Don't you think ? – Joze Jun 13 '11 at 13:10
  • @Joze: The sample code you've given still won't compile. We won't know what you need to do until you've shown us code which gives the warning you're talking about. – Jon Skeet Jun 13 '11 at 13:14
  • I tried... Apparently my accessors wasn't well describe => Warning Sorry for the inconvenience... Thank you all :) is there a way to "close" the topic ? – Joze Jun 13 '11 at 13:29
0

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lester
  • 1,112
  • 2
  • 15
  • 22