No, we dont't like split.



  • I'm working in a project that uses mostly VB.NET.

    One day when debugging I found myself in class called Utility
    In this class there are a number of slightly different methods like the one below.
    Not the most efficient code I've seen.

    With split you could do the same thing in one line.
    Looking at the code it looks like it's ported from VB6, but that's no excuse, VB6 has split.
     

      '*****************************************************************
        ' Name          xGetItem
        ' Purpose       Returns an item from a string
        ' Parameters
        '               sSource The string to get the item from
        '               sDelm  the item delimiter
        '               n       The (1-based) index for the item to get
        '
        ' Note:         If  sDelm is blank, "," will be used, The order of the
        '               parameters is different than in most other string functions
        '
        ' Example       xGetItem("a,z,x,d,f",3) equals "x"
        ' Returns       The nth item as a string
        '*****************************************************************

        Public Function xGetItem(ByVal sSource As String, ByVal n As Integer, Optional ByVal sDelm As String = ",") As String
            On Error GoTo ErrorHandler
            Dim i As Integer
            Dim j As Integer
            Dim k As Integer
            Dim nCount As Integer
            Dim nLspos As Integer
            Dim nLength As Integer
            Dim sDest As String

            xGetItem = ""
            sDest = ""
            nCount = 0
            nLspos = 1

            If (sDelm = " ") Then
                xGetItem = xGetToken(sSource, n)
                Exit Function
            End If

            nLength = Len(sSource)

            For i = 1 To nLength
                If (Mid(sSource, i, 1) = sDelm) Then
                    nCount = nCount + 1

                    If (n = nCount) Then
                        k = 1
                        j = nLspos

                        While ((j <= nLength) And (Mid(sSource, j, 1) <> sDelm))
                            sDest = sDest & Mid(sSource, j, 1)
                            j = j + 1
                            k = k + 1
                        End While

                        xGetItem = sDest
                        Exit Function
                    Else
                        nLspos = i + 1
                    End If
                End If
            Next

            nCount = nCount + 1

            If (n = nCount) Then
                k = 1
                j = nLspos

                While ((j <= nLength) And (Mid(sSource, j, 1) <> sDelm))
                    sDest = sDest & Mid(sSource, j, 1)
                    j = j + 1
                    k = k + 1
                End While
            End If

            xGetItem = sDest
            Exit Function

    ErrorHandler:
            RaiseError(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)
        End Function 'xGetItem



  • Wow.

    Just to save my eyes here:

    Public Function xGetItem(ByVal sSource As String, ByVal n As Integer, Optional ByVal sDelm As String = ",") As String
            Return sSource.Split(sDelm).GetValue(n - 1)
    End Function

    Bonus points for using optional func arguments, a WTF in itself. 



  • The Real WTF(tm) is VB.net </obligatory>

     



  • They could have used XML documentation to make other people's lives easier. Also, they forgot to put in the comments a remark saying "we know what you're thinking, but we like to do things our way".

    You think you had it though? A former boss of mine wanted to reimplement the DataTable class.



  • Please tell me this is a lame "conversion" to .NET from a much older dialect of VB (and which ones didn't have split? Pre-VB6, I hope?)



  • That would have to be some really old stuff... I remember VB4 having it (though I could be wrong here).



  • Does... 


    ErrorHandler:
            RaiseError(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)

    ...mean what I think it does?

    I don't know as much VB as I'd like*.

     

    <font size="1">*I know a little**.</font>

    <font size="1">**Which is more than I wanted. </font>



  • @Zecc said:

    Does... 


    ErrorHandler:
            RaiseError(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)

    ...mean what I think it does?

    I don't know as much VB as I'd like*.

    It depends what you think it does... :-)

    At the top you have the: On Error goto ErrorHandler.
    So when you get an exception the code goes to the ErrorHandler label. The RaiseError logs the error and then calls Err.Raise (VB6 style).
    They could have used try/catch, but that would have been to obvious...

    And to be fair, the code outside of this class is mostly OK. This is a dark corner of the code where no one looks.


     

     

     



  • Oh, I thought it was another classic example of <font face="courier new,courier" size="2">try{...} catch(e){ throw e; }</font>, but if it logs the exception I guess that's okay.



  • @Control_Alt_Kaboom said:

    The Real WTF(tm) is VB.net </obligatory>

    Nope, not even close.

    @Control_Alt_Kaboom said:
    =^.^= A Furry & proud of it.


    That's the real wtf!



    SCNR! ;-)


Log in to reply