0

Is it possible to prevent compiler warning caused by a static factory method returning an instance of an obsolete class?

For example if an instance of an obsolete class (Foo) is created through a static method (Create) a compiler warning is caused by the factory method.

<Obsolete()> _
Public Class Foo

    Public Shared Function Create() As Foo
        ' Instantiate and return
    End Function
End Class

In C# the warning could be suppressed using "#pragma warning..." but I don't think that this exists within VB.Net. Migrating to C# is not an option due to business requirements.

detaylor
  • 7,112
  • 1
  • 27
  • 46

1 Answers1

0

I think you have to open the .vbproj-file with notepad, search the NoWarn-Tag and add the error-id there(618 should be obsolete-warning):

<NoWarn>618,42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 618 is the error code for the C# compiler, in VB it would be 40000. Unfortunately this would prevent the warning everywhere within the project, not just the error caused by the factory method. I was looking for something that would prevent the warning only within the factory method. The only option that I can think of is to raise the obsolete message as an error and then remove the factory method. This would then be a breaking change which I would prefer to avoid. – detaylor Jan 21 '11 at 14:12