Adding attribute to an inherited property

adriaan

Member
Joined
Feb 28, 2005
Messages
13
Programming Experience
5-10
Adding attribute to an inherited property [RESOLVED]

Hi,

Is it possible to add an attribute to and inherited property, or do you need to override the property if you want to add an attribute to the property?

If possible how do you do that, you see I want to use inheritance for code re-use, but for some properties i only want to add an attribute.

Thanks,

Adriaan
 
Last edited:
Attributes are declarative tags that can be used to annotate types or class members, thereby modifying their meaning or customizing their behavior. This descriptive information provided by the attribute is stored as metadata in a .NET assembly and can be extracted either at design time or at runtime using reflection.

As an item of metadata, the attribute describes the program element to which it applies and is available through reflection both at design time (if a graphical environment such as Visual Studio .NET is used), at compile time (when the compiler can use it to modify, customize, or extend the compiler's basic operation), and at runtime (when it can be used by the Common Language Runtime or by other executable code to modify the code's ordinary runtime behavior).

But in this case he probably want to add property that is something different than atribute. However here we go. Say you want to add newpropertyto the TextBox control:
Of course that you should first inherit TexbBox before you start to change/add its properties

For instance:
VB.NET:
 Public Class newProperty 
Inherits System.Windows.Forms.TextBox
 
Private a_Language As Language

VB.NET:
'set the language for error msg first line below is attribute
<Description("'set the language for error msg"), Category("myNewCayegory")> _
Public Property Language() As Languages 
Get
Return a_Language
End Get
Set(ByVal Value As Languages)
a_Language = Value
End Set
End Property

VB.NET:
 'supported languages 
Enum Languages
English = 0
Macedonian = 1
End Enum

Cheers ;)

 
If you do want to add an attribute to an inherited property then you do need to override or shadow the property but then, in the child implementation, simply call the parent implementation, e.g.
VB.NET:
<Description("This is the description.")> Public Overrides Property MyProperty As String
	Get
		Return MyBase.MyProperty
	End Get
	Set(ByVal Value As String)
		MyBase.MyProperty = Value
	End Set
End Property
 
Back
Top