VentureFree
Well-known member
- Joined
- Jan 9, 2008
- Messages
- 54
- Programming Experience
- 5-10
A former co-worker wrote a class in C# that was to be used as an attribute. I'm having to convert his work to VB (yeah, it's pointless and way more work than necessary, but management thinks it's a good idea). Unfortunately I'm having an issue because one of the main properties of the class is an ArrayList which is set with a ParamArray in the constructor. The class looks like this (note I've tried to abstract out the problem, so this is a simplified version of the class only)
To use this, I would be adding the attribute to shared variables as follows:
In the first one, there is just the single label being added. No Problem. In the second one there are 2 different labels being added. Again, no problem. The third one, however, is a problem because it's assuming that the "Description", which is supposed to be the description is instead being interpreted as another label.
This wasn't a problem in C# because it could be set as follows:
Of course this doesn't work in VB because optional parameters aren't referred to by name as they are in C#. As a stopgap I've changed the constructor to allow only a single label like this
but that undermines some of what this class is meant to do. Does anyone know how, or even if I can get the same basic functionality to work in VB?
I can't help but think there must be a way to make the existing design work. Imagine this class was built into a library that I could not modify. Would I be forever forbidden from setting Description because of this design? One would hope not. But maybe I'm wrong and it's just bad design to begin with.
Anyway, thanks for any help that you might be able to offer.
VB.NET:
<AttributeUsage(AttributeTargets.Field Or AttributeTargets.[Property])> _
Public Class LabelAttribute
Inherits Attribute
Public Sub New()
m_Labels = New ArrayList()
End Sub 'New
Public Sub New(ByVal ParamArray NewLabels As [String]())
m_Labels = New ArrayList(NewLabels)
End Sub 'New
Private m_Labels As ArrayList = Nothing
Private m_Description As [String] = Nothing
Public ReadOnly Property Labels() As ArrayList
Get
Return m_Labels
End Get
End Property 'Labels
Public Property Description() As [String]
Get
Return m_Description
End Get
Set(ByVal value As [String])
m_Description = value
End Set
End Property 'Description
End Class 'LabelsAttribute
VB.NET:
<Label("Label1")> _
Private Shared s_MyFirstVariable As [String]
<Label("Label1", "Label2")> _
Private Shared s_MySecondVariable As [String]
' Broken! "Description" should be the description, not another label
<Label("Label1", "Label2", "Description")> _
Private Shared s_MyThirdVariable As [String]
This wasn't a problem in C# because it could be set as follows:
VB.NET:
[Label("Label1", "Label2", Description="Description")]
private static string s_MyThirdVariable;
VB.NET:
Public Sub New(ByVal NewLabel As [String])
m_Labels = New ArrayList()
m_Labels.Add(NewLabel)
End Sub 'New
I can't help but think there must be a way to make the existing design work. Imagine this class was built into a library that I could not modify. Would I be forever forbidden from setting Description because of this design? One would hope not. But maybe I'm wrong and it's just bad design to begin with.
Anyway, thanks for any help that you might be able to offer.