Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding a piece of code

Tags:

vb.net

vb6

I do C#, no experince with VB and I don't have any VB installed. I've been looking at the code below to understand how it works, could someone take a look at this?

So if I call this method with Cat and cat and don't pass the optional parameter, does it return true and says Cat and cat are equal?

Public Function AreStringsEqual(ByRef sString1 As String, ByRef sString2 As String, Optional ByVal eCompareMethod As VbCompareMethod = vbBinaryCompare) As Boolean

  If LenB(sString1) = LenB(sString2) Then
    If LenB(sString1) = 0 Then
      AreStringsEqual = True
    ElseIf eCompareMethod = vbBinaryCompare Then
      AreStringsEqual = (InStrB(1, sString1, sString2, eCompareMethod) <> 0)
    Else
      AreStringsEqual = (StrComp(sString1, sString2, eCompareMethod) = 0)
    End If
  End If
End Function
like image 983
DarkNightFan Avatar asked Dec 16 '22 17:12

DarkNightFan


1 Answers

That’s some weird code. Anyway, the default for the third argument is vbBinaryCompare which means that "Cat" and "cat" will compare unequal. To have them compare equal, you’d need to pass vbTextCompare.

Now here’s why the code is weird: it’s utterly redundant. You could just call StrComp directly.

like image 73
Konrad Rudolph Avatar answered Dec 21 '22 23:12

Konrad Rudolph