I’m writing some code to turn the contents of class modules into an XML file for Affordable Care Act compliance purposes. The XML file spec says that my flag for whether the dependent is a spouse is “Y” or “N”. In my class, I have a Relation property that can be “Son”, “Daughter”, or “Spouse”. I made a new property to return the “Y” or “N”.
1 2 3 4 5 6 7 8 9 |
Public Property Get IsSpouseXML() As String If Me.Relation = "Spouse" Then IsSpouseXML = "Y" Else IsSpouseXML = "N" End If End Property |
I hate writing all those lines to convert a Boolean into something else. I know it’s not that big of a deal, but it just bugs me. So I fixed it.
1 2 3 4 5 |
Public Property Get IsSpouseXML() As String IsSpouseXML = Split("N Y")(Abs(Me.Relation = "Spouse")) End Property |
Now that’s fancy. The comparison is made and the True or False is converted to a Long via the Abs() function (to turn True to 1 instead of -1) and the proper element of the array is selected. It’s still not good enough.
1 2 3 4 5 6 7 8 9 10 11 |
Public Property Get IsSpouse() As Boolean IsSpouse = Me.Relation = "Spouse" End Property Public Property Get IsSpouseXML() As String IsSpouseXML = Split("N Y")(Abs(Me.IsSpouse)) End Property |
Yeah, that’s better. But it’s so specific to spouses. Spouse is a dependent that gets special attention, so I don’t mind having a dedicated property to it. It’s appropriate for the domain, I think. But if I wanted to really generalize the hell out of it, I might make an IsRelation property and then take my conversion property into a function.
1 2 3 4 5 6 7 8 9 10 11 |
Public Property Get IsRelation(ByVal sRelation As String) As Boolean IsRelation = Me.Relation = sRelation End Property Public Function ConvertBool(bValue As Boolean, vArr As Variant) As String ConvertBool = vArr(Abs(bValue)) End Function |
Now I can have complete customization of the return string.
1 2 3 4 5 6 7 8 9 |
Public Sub TEST_IsSpouse() Dim clsDep As CDependent For Each clsDep In gclsEmployees.Employee(4).Dependents Debug.Print ConvertBool(clsDep.IsRelation("Spouse"), Array("Not so much", "Of course")), clsDep.Relation Next clsDep End Sub |