Hey!

We have a situation where we are using data from a service that is from a non normalized database. In this particular case we are trying to create information to a graph component on what axis to draw and what units. One axix can be displayed with two different units (us and SI) This information is stored in the database as an integer where 1 = SI, 2=US and 12 or 21 is both. Unit is an enum with SI, US or BothSIAndUS

This small method is what we have dreamt up so far

Code:
Private Function GetUnitFromInteger(unitsforAxis As Integer) As Unit
        Dim ints = unitsforAxis.ToString.ToArray().Distinct()
        
        If ints.Count = 2 Then
            Return Unit.BothSIAndUS
        ElseIf ints.Count = 1 Then
            If ints.First() = "1" Then
                Return Unit.SI
            ElseIf ints.First() = "2" Then
                Return Unit.US
            Else
                Throw New NotImplementedException("There is no support for unit: " & ints.First())
            End If
        Else
            Throw New Exception("The integer can either be 1, 2 or 12, or 21")
        End If
    End Function
The code feels kind of crappy. Is there a better more clean way to parse information of this kind? The code does work but ther are lots of Ifs and magic numbers.

We have 10 more kinds of similar data where many settings have been cramped into a single integer or string that needs to be splitted into proper objects. We try to do this as close to the db as possible to avoid lots of string parsing in our services or viewmodels.

this is a really old database, and for now we only support 1, 2, 12, 21, everything else is supposed to throw an exception and the users have to fix it manually.

/H