473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Custom Attributes

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
Nov 21 '05 #1
3 1209

"Dan" <da********@NOSPAMvirgin.net> wrote
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

Nov 21 '05 #2
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

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:Oe*************@TK2MSFTNGP11.phx.gbl...

"Dan" <da********@NOSPAMvirgin.net> wrote
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

Nov 21 '05 #3

"Dan" <da********@NOSPAMvirgin.net> wrote in message
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
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: ...
1
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
1
by: Tamas Demjen | last post by:
I started to experiment with VC++ 2005 Beta1. So far everything went fine, and already have a working project, but soon I realized that the compiler was ancient (not supporting half of the C++/CLI...
3
by: The Developer | last post by:
Hi All, I have a web application where I am adding a custom attribute to my ASP.NET text box control and changing value of that attribute at client side using JavaScript. My problem is that...
0
by: Sanjay Pais | last post by:
This is an extension of an earlier post I have created a custom text box and compiled it and dropped it into my toolbox. However when I change the value of my custom property in design mode and...
9
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
1
camel
by: camel | last post by:
Apologies if foolish question, but I somewhat expected Custom Attributes (i.e. inheriting from System.Attribute) decorating class properties to come through via WSDL to the proxy the same as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.