Custom Attributes

D

Dan

Hi,

I am messing around with custom attributes and may have understood the
concept a bit wrong:-

I have a simple class that has a few public properties that individually
have custom attributes. For example, each property has a custom attribute
class with one property defined, that of IsChanged.

I know how to initialise the attribute class with a value (i.e. define a
constructor with an argument and use that to assign to the IsChanged
property), but how do I access the attribute from within the class property
subsequently? The thing I would like to be able to do is modify the
attribute IsChanged when someone modifies the class property. Further to
that, I want to be able to check on those attributes later on in other
routines within the same class.

Sample code:-

Imports System.Reflection

<AttributeUsage(AttributeTargets.Property)> _
Class MyAttribute
Inherits Attribute

Private _isChanged As String

Public Property IsChanged() As String
Get
Return _isChanged
End Get
Set(ByVal Value As String)
_isChanged = Value
End Set
End Property

Sub New(ByVal isChanged As String)
_isChanged = isChanged
End Sub

End Class

Class TestAttr

Private _testProperty As String

<MyAttribute("No")> _
Public Property testProperty() As String
Get
Return _testProperty
End Get
Set(ByVal Value As String)
_testProperty = Value
' set the isChanged attribute to Yes
End Set
End Property

Sub checkTestProperty()
'check isChanged and do something based upon value
End Sub

End Class


Can anyone help?

Cheers
Dan
 
L

Larry Serflaten

Dan said:
I am messing around with custom attributes and may have understood the
concept a bit wrong:-

I just wanted to mention that using attributes as properties does not
seem like the right thing to do. I am under the impression that attributes
should decribe the behaviour of the property and not be used as
properties themselves. If you really needed an IsChanged behaviour
on several individual properties, individually, I'd suggest you use a
structure for those values:

Private Structure StringProperty
Public Text As String
Public IsChanged As Boolean
End Structure

Private myTestProperty As StringProperty


Public TestProperty As String
Get
Return myTestProperty.Text
End Get
Set(ByVal Value As String)
myTestProperty.IsChanged = (Value <> myTestProperty.Text)
If myTestProperty.IsChanged Then myTestProperty.Text = Value
End Set
End Property


HTH
LFS
 
D

Dan

would i then be able to inherit the structure for all properties of a class
so that i wouldnt have to declare a separate one for each property?

cheers
dan
 
L

Larry Serflaten

Dan said:
would i then be able to inherit the structure for all properties of a class
so that i wouldnt have to declare a separate one for each property?

What do you mean?

LFS
 

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

Top