Custom Attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    Custom Attributes

    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.Reflecti on

    <AttributeUsage (AttributeTarge ts.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("N o")> _
    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 checkTestProper ty()
    'check isChanged and do something based upon value
    End Sub

    End Class


    Can anyone help?

    Cheers
    Dan


  • Larry Serflaten

    #2
    Re: Custom Attributes


    "Dan" <dan.anselm@NOS PAMvirgin.net> wrote
    [color=blue]
    > I am messing around with custom attributes and may have understood the
    > concept a bit wrong:-[/color]

    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

    Comment

    • Dan

      #3
      Re: Custom Attributes

      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



      "Larry Serflaten" <serflaten@usin ternet.com> wrote in message
      news:Oe6PJgHsEH A.596@TK2MSFTNG P11.phx.gbl...[color=blue]
      >
      > "Dan" <dan.anselm@NOS PAMvirgin.net> wrote
      >[color=green]
      >> I am messing around with custom attributes and may have understood the
      >> concept a bit wrong:-[/color]
      >
      > 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
      >[/color]


      Comment

      • Larry Serflaten

        #4
        Re: Custom Attributes


        "Dan" <dan.anselm@NOS PAMvirgin.net> wrote in message[color=blue]
        > 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?[/color]

        What do you mean?

        LFS

        Comment

        Working...