How can I get rid of "warning CS0649 : field is never assigned to"

  • Thread starter Thread starter Andrew Backer
  • Start date Start date
A

Andrew Backer

I have a series of class variables that are assigned to through
reflection. Every time I compile one of these classes I get this
warning :

warning CS0649: Field 'xxx' is never assigned to, and will always have
its default value null

Is there any way I can supress this warning? I have a custom attribute
applied to each field, which the binder uses, and am explicitly
assigning to null. I would like to remove the '=null;' if I can.

[BindMe] protected TextBox theText = null;

ASP.Net pages do not have this problem when you place controls on them,
so I think this may be possible. The only post addressing this
directly is from 2002, and in this group.

I was hoping for something like [CompilerDisableWarning(649)] that I
could apply to my custom attributes.

Thanks for any help,
- Andrew Backer
 
Andrew said:
I have a series of class variables that are assigned to through
reflection. Every time I compile one of these classes I get this
warning :

warning CS0649: Field 'xxx' is never assigned to, and will always have
its default value null

Is there any way I can supress this warning? I have a custom attribute
applied to each field, which the binder uses, and am explicitly
assigning to null. I would like to remove the '=null;' if I can.

[BindMe] protected TextBox theText = null;

ASP.Net pages do not have this problem when you place controls on them,
so I think this may be possible. The only post addressing this
directly is from 2002, and in this group.

I was hoping for something like [CompilerDisableWarning(649)] that I
could apply to my custom attributes.

Thanks for any help,
- Andrew Backer
not sure for c#, but in visual c you can #pragma disable warning(CS0649)
I don't remember the syntax, but that's the idea...

you may try to look up how to do that in c#

marcel
 
Yes, there is something similar for c#, I think you even have the
syntax correct :)

I don't want to stick pragmas like that in each class that does this,
though. The current work aorund is to just assign = null.

I am hoping that there is some language/framework feature that will let
me disable this warning in much the same way custom attributes work. I
could then combine my attribute with this base, and be able to ignore
individual fields by tagging them...

[BindMe]
[IgnoreWarning(964)]
protected TextBox myField;

[BindMeAndIgnoreWarnings]
protected TextBox myField;

There must be a clever way to do this, since aspx pages do it. Perhaps
there is some other detail that I am missing that makes this not apply?
Any smartie from MS have any clues?

- Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top