What is the most efficient way to discriminate between empty and null value? I want to:
CStr(str)
to True
when str=""
, whereasCStr(str)
to False
when str=Nothing
Here it is:
<Runtime.CompilerServices.Extension>
Public Function HasValue(s As String)
Return TypeOf (s) Is String
End Function
Slightly Better equivalent: (from answer of jmcilhinney)
<Runtime.CompilerServices.Extension>
Public Function HasValue(s As String)
Return s IsNot Nothing
End Function
Also a benchmark of various methods on 10000 strings of various length:
Function(x As String)..............:Total Time (rel Efficency %)
TypeName(x) = "String".....................:0.850ms (17.1%)
VarType(x) = VariantType.String........:0.590ms (24.6%)
TypeOf (x) Is String........................... :0.150ms (96.7%)
x IsNot Nothing................................. :0.145ms (100%)