|I'm writing an update to a software package that takes binary data from an instrument and creates a spreadsheet. There is a column that list a reading mode that in the older version of the software outputted either "TC" or "QM". Now in the updated version I want it to write something more descriptive; that being either "Time Corrected" or "Quick Mode", but I still want to maintain backwards compatibility.
I thought the best way to do this would be to use custom attributes where the new description and old description would be saved and then based on the settings of the software the program could choose the proper description.
First I created a custom attribute class:
<AttributeUsage(AttributeTargets.Field, AllowMultiple:=True, Inherited:=False)>
Public Class OutputDescriptionAttribute
Private _oldDescription As String
Private _newDescription As String
Public Sub New(oldDescription As String, newDescription As String)
Me._oldDescription = oldDescription
Me._newDescription = newDescription
Then I created the enum and applied the attributes:
Public Enum ReadingModeType
<OutputDescription("TC", "TimeCorrected")> TimeCorrected
<OutputDescription("QM", "QuickMode")> QuickMode
Lastly I copied a method off of Microsoft's website that is supposed to get the values from custom attributes:
Public Shared Sub GetDescription(t As Type)
Dim MyAttribute As OutputDescriptionAttribute =
CType(Attribute.GetCustomAttribute(t, GetType(OutputDescriptionAttribute)), OutputDescriptionAttribute)
If MyAttribute Is Nothing Then
Console.WriteLine("The attribute was not found.")
I got this method from http://msdn.microsoft.com/en-us/library/71s1zwct.aspx[^]. I then called this method like so:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim s As ReadingModeType = ReadingModeType.QuickMode
However when I step through the code in the
GetDescription method, the
MyAttribute object returns nothing. This is my first time working with custom attributes so I know I'm probably missing something very simple. Can anyone please advise me how I can retrieve the descriptions for the attribute applied to the specified enum value?
Thanks in advance.