When to use Attributes????

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 65
Reputation: MxDev has a little shameless behaviour in the past 
Solved Threads: 1
MxDev MxDev is offline Offline
Junior Poster in Training

When to use Attributes????

 
0
  #1
Sep 29th, 2009
Hi dudes,
Many times when I read others codes I found they're using attributes , and actually I don't know when to use attributes, or when it becomes necessary to use it to be aware of it in my written codes. So, would some one here help me.

Any help appreciated, thanks a lot guys.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,242
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 577
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: When to use Attributes????

 
2
  #2
Sep 29th, 2009
It helps organize or flag members of a class. If you're serializing a class you may want to mark the members you want to include in serialization with an attribute, or maybe your logic serializes all public members and you only want to ignore certain members with the XmlIgnore attribute. I wrote a data layer where I "bind" a class to an SQL Table with attributes:

  1. [DBTableInfo("vea_Alarm")]
  2. public sealed class Alarm : SqlObject
  3. {
  4. [DBFieldInfo("AlarmId", SqlDbType.Int, PrimaryKey=true, Identity=true)]
  5. private int _alarmId = 0;
  6. [DBFieldInfo("EventId", SqlDbType.Int)]
  7. private int _eventId;
  8. [DBFieldInfo("ServerId", SqlDbType.Int)]
  9. private int _serverId;
  10. [DBFieldInfo("SensorId", SqlDbType.Int)]
  11. private int _sensorId;
  12. [DBFieldInfo("SensorRow", SqlDbType.Int)]
  13. private int _sensorRow;
  14. [DBFieldInfo("IOPointNum", SqlDbType.Int)]
  15. private int _IOPointNum;
  16. [DBFieldInfo("EventNum", SqlDbType.Int)]
  17. private int _eventNum;
  18. [DBFieldInfo("StartDate", SqlDbType.DateTime)]
  19. private DateTime _startDate;
  20. [DBFieldInfo("EndDate", SqlDbType.DateTime)]
  21. private DateTime _endDate;
  22. [DBFieldInfo("CorrectedDate", SqlDbType.DateTime)]
  23. private DateTime _correctedDate;
  24. [DBFieldInfo("State", SqlDbType.VarChar, Size=DataLength.State)]
  25. private string _state;
  26. [DBFieldInfo("Alarm", SqlDbType.Bit)]
  27. private bool _alarm;
  28. [DBFieldInfo("Closed", SqlDbType.Bit)]
  29. private bool _closed;
  30. [DBFieldInfo("Corrected", SqlDbType.Bit)]
  31. private bool _corrected;
  32. [DBFieldInfo("IOName", SqlDbType.VarChar, Size=DataLength.IOName)]
  33. private string _ioName;

The code automatically generates a Select, Insert, Update, and Delete queries based on the attributes it finds.

Maybe you want to control serialization:
  1. [Serializable]
  2. public sealed class SeriesTag
  3. {
  4. public SeriesTag()
  5. {
  6. this.Identifier = string.Empty;
  7. this.TrendIndex = 0;
  8. this.DataSourceGUID = string.Empty;
  9. this.SeriesGUID = string.Empty;
  10. }
  11. public SeriesTag(Series Parent)
  12. : this()
  13. {
  14. this.Parent = Parent;
  15. }
  16. [XmlIgnore]
  17. public Series Parent { get; set; }

Its just a handy way of going about things at runtime without having to maintain lists. You can reflect members and look for attributes.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC