I'm working on migrating some old VB6 code to C# at the moment, I've been testing my C# code against the VB code to check that the counterpart methods in each language return the same value.
I'm having problems with this if statement:
If InStr(1, "LPTVJY", strTempTaxCode) <> 0 Then
strTempTaxCode = "0" & strTempTaxCode
End if
When strTtempTaxCode = ""
The value 1
is returned from the InStr(1, "LPTVJY", strTempTaxCode)
call. Why is this? As far as I am aware I should be returned 0
as none of the characters from "LPTVJY"
are within strTempTaxCode
All strings of nonzero length, including single-character strings, contain the empty string.
Debug.Print InStr("abc", "") ' => 1
Debug.Print InStr("a", "") ' => 1
Debug.Print "abc" & "" = "abc" ' => True
If you have trouble wrapping your head around the concept, consider that all numbers have a factor of 1:
' (not a real function)
Debug.Print Factors(2) ' => 1, 2
Debug.Print 2 * 1 = 2 ' => True
This is known as the Multiplicative Identity Property. You can think of strings as having a similar identity property involving the empty string.
If you don't want InStr()
to match the empty string, do a preliminary test before calling it:
If Len(strTempTaxCode) > 0 Then
If InStr(1, "LPTVJY", strTempTaxCode) <> 0 Then
strTempTaxCode = "0" & strTempTaxCode
End If
End If