Export (0) Print
Expand All

ObsoleteAttribute.IsError Property

Gets a Boolean value indicating whether the compiler will treat usage of the obsolete program element as an error.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public ReadOnly Property IsError As Boolean

Property Value

Type: System.Boolean
true if the obsolete element usage is considered an error; otherwise, false. The default is false.

The following example defines a class that contains two members marked as obsolete. The first, a property named OldProperty, produces a compiler warning if it is called. The second, a method named CallOldMethod, produces a compiler error. The example uses reflection to get information about the ObsoleteAttribute attributes applied to members of the type and displays the values of their Message and IsError properties.

Imports System.Reflection

Public Module Example
   ' Mark OldProperty As Obsolete.
   <ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", False)> 
   Public ReadOnly Property OldProperty As String 
      Get 
         Return "The old property value." 
      End Get 
   End Property 

   Public ReadOnly Property NewProperty As String 
      Get 
         Return "The new property value." 
      End Get 
   End Property 

   ' Mark OldMethod As Obsolete.
   <ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", True)> 
   Public Function OldMethod() As String 
      Return "You have called OldMethod." 
   End Function 

   Public Function NewMethod() As String    
      Return "You have called NewMethod." 
   End Function    

   Public Sub Main()
      ' Get all public members of this type. 
      Dim members() As MemberInfo = GetType(Example).GetMembers()
      ' Count total obsolete members. 
      Dim n As Integer = 0

      ' Try to get the ObsoleteAttribute for each public member.
      Console.WriteLine("Obsolete members in the Example class:")
      Console.WriteLine()
      For Each member In members
         Dim attribs() As ObsoleteAttribute = CType(member.GetCustomAttributes(GetType(ObsoleteAttribute), 
                                                                             False), ObsoleteAttribute())
         If attribs.Length > 0 Then 
            Dim attrib As ObsoleteAttribute = attribs(0)
            Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name)
            Console.WriteLine("   Message: {0}", attrib.Message)             
            Console.WriteLine("   Warning/Error: {0}", if(attrib.IsError, "Error", "Warning"))      
            n += 1
         End If 
      Next 

      If n = 0 Then
         Console.WriteLine("The Example type has no obsolete attributes.")
      End If  
   End Sub   
End Module 
' The example displays the following output: 
'       Obsolete members in the Example class: 
'        
'       Member Name: Example.OldMethod 
'          Message: This method is obsolete. Call NewMethod instead. 
'          Warning/Error: Error 
'       Member Name: Example.OldProperty 
'          Message: This property is obsolete. Use NewProperty instead. 
'          Warning/Error: Warning

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft