Bobby
Bobby

Reputation: 21

How to avoid warning CS0649 while using FileHelpers Module

I am using the FileHelpers nuget to read the files. It works as excepted but it throws me a warning when I tried to debug in Visual Studio.

How to get rid of warning CS0649: Field 'Orders.Freight' is never assigned to, and will always have its default value null ?

 class Orders : INotifyRead
{
    [FieldFixedLength(10)]
    public string Freight;
    public void BeforeRead(BeforeReadEventArgs e)
    {
        if (e.RecordLine.StartsWith("Machine"))
            // ||
            // e.RecordLine.StartsWith("-"))
            e.SkipThisRecord = true;
    }
    public void AfterRead(AfterReadEventArgs e)
    {
        //  we want to drop all records with no freight
        if (Freight == "_raw")
            e.SkipThisRecord = true;
    }
}

Upvotes: 0

Views: 1067

Answers (3)

blins
blins

Reputation: 2535

You essentially have two choices and which way to go really depends on the intent (to suggest one or the other is subjective). First, you could eliminate the warning if the design requirement of your Orders type dictates that it should have a null default value.

public string Freight = null;

The above merely clarifies that intent and therefore eliminates the warning.

The alternative is to suppress the warning as the other answers mention. In your case, if the assumption is that the value should have been set via Reflection then this alternative seems reasonable if not preferable in such a case.

Upvotes: 0

Thumper
Thumper

Reputation: 532

For the sake of completeness, I'm just going to combine blins' answer and Mike's answer - nothing original, just trying to help the next person who runs across this page.

Per blins: You may set the value equal to null and the first warning "Field XYZ is assigned to but never used"

public string Freight = null; //or = "", or = default(string) (which is null)

Per Mike, the "magic" he's talking about is Reflection. The variable is assigned to at runtime. This is something the compiler doesn't detect. More on Mike's answer about suppressing the warning found here: Suppressing "is never used" and "is never assigned to" warnings in C#

To suppress warnings for "Field XYZ is never used", you do this:

#pragma warning disable 0169
... field declaration
#pragma warning restore 0169

To suppress warnings for "Field XYZ is never assigned to, and will always have its default value XX", you do this:

#pragma warning disable 0649
... field declaration
#pragma warning restore 0649

Upvotes: 1

Mike Nakis
Mike Nakis

Reputation: 61993

No, do not explicitly assign a default value to Freight.

The warning is legitimate, because you never really assign a value to the field.

You do not assign a value, because the field gets populated by magic. (Incidentally, that's why I do not like magic; but that's a different story altogether.)

So, the best approach is to acknowledge the fact that the warning is legitimate but accounted for, and to explicitly suppress it.

So, take a look at the documentation of the #pragma warn directive: https://msdn.microsoft.com/en-us/library/441722ys.aspx

Upvotes: 4

Related Questions