Multiple parameters for a method (only one to be used)

Kem, sorry about that. I don’t know where I picked up the idea of .Len. For the fun of it, and for others, I created a simple app and profiled it. It looped 100,000 assigning a isEmpty Boolean value using two methods:

[code] Dim isEmpty As Boolean

For i As Integer = 0 To ITERATIONS
isEmpty = (value = “”)
Next[/code]

and

[code] Dim isEmpty As Boolean

For i As Integer = 0 To ITERATIONS
isEmpty = (value.Len = 0)
Next[/code]

As Kem said, the Equal variant was much faster. LenTest = 23ms while the EqualTest = 8ms.

Now, for those new to profiling, when profiling it slows down your code, but slows down everything equally. It just has to track a lot more during runtime. So in real life, both of those will be faster but the proportion should stay about the same.

So… use = as Kem says!

[quote=81541:@Jeremy Cowgar]…

[code]Function IsEmpty(Extends value As String) As Boolean
Return (value.Len = 0)
End Function

Function IsEmpty(Extends w As TextBox) As Boolean
Return w.Text.IsEmpty
End Function
…
// etc
[/code]….[/quote]
Cool!

Thanks for testing that. I don’t know what you used for value, but when it has a lot of text, the difference is pronounced with a multi-byte encoding. Len has to go through the bytes and count the actual characters, and that will take time.

FYI, I used to use value.LenB=0 until this was pointed out to me too.

[quote=81580:@Kem Tekinay]Thanks for testing that. I don’t know what you used for value, but when it has a lot of text, the difference is pronounced with a multi-byte encoding. Len has to go through the bytes and count the actual characters, and that will take time.

FYI, I used to use value.LenB=0 until this was pointed out to me too.[/quote]

Kem, I should have said. I used a 70 character long string. It looked like he was testing control inputs in general, so I kept it something sensible. I’m sure notes could be part of that (TextArea) but 70 is pretty generous for field inputs. Anyway, I guess the underlying thing we can determine from that is that the String object (internally) does not store its length. It must iterate through until it finds a NULL character or some other end of string marker, counting as it goes. That compared to a string compare to empty, it will only look up until something doesn’t match, in this case it it makes it a constant time as it will always compare to nothing.

I tend to like question methods when it comes to booleans, so I also have HasContent which does the inverse, instead of If Not value.IsEmpty Then …