Obsolete

 <  Day Day Up  >  

Over time, it is inevitable that types and type members in an assembly will become obsolete or no longer used. Just removing obsolete code from an assembly, however, is an abrupt all-or-nothing transition ”any application that depends on the obsolete code will immediately cease to run, and any code that depends on the obsolete code will fail to compile. To help developers gradually obsolete types and type members , the .NET Framework defines an attribute, System.ObsoleteAttribute , that can be applied to declarations to indicate that a type or type member is obsolete. When a type or type member that is marked with the attribute is used in a program, the Visual Basic .NET compiler gives a warning or an error (depending on how the attribute is applied) and prints the message specified in the attribute. For example:

 Class Customer   Public Name As String   <Obsolete("Call Print(True) instead.")> _   Public Sub Print()     Print(True)   End Sub   Public Sub Print(ByVal WriteLine As Boolean)     If WriteLine Then       Console.WriteLine(Name)     Else       Console.Write(Name)     End If   End Sub End Class Module Test   Sub Main()     Dim c As Customer = New Customer()     ' Prints out the warning: "Call Print(True) instead."     c.Print()   End Sub End Module 

In this example, the Customer.Print method that takes no parameters has been replaced with a new method that takes a Boolean parameter. Because the obsolete method should eventually be removed, an Obsolete attribute is applied to the obsolete method. This causes a message to be displayed when the method is used, pointing developers to the new method.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net