-3

What is the most efficient way to discriminate between empty and null value? I want to:

  1. evaluate CStr(str) to True when str="", whereas
  2. evaluate CStr(str) to False when str=Nothing
anefeletos
  • 672
  • 7
  • 19
  • 1
    I'm not sure, is this a question? – Sasha Jul 18 '18 at 08:09
  • Stack Overflow supports and encourages answering your own questions to help other users. The question is how to discriminate between null and zero length string. Its usefull to avoid errors when passing strings to functions. – anefeletos Jul 18 '18 at 08:13
  • It is definetely not duplicate. The question is about discriminating CStr(nothing) from CStr(""). – anefeletos Jul 18 '18 at 12:48
  • But if your requirement is still to be able to write something like `If(str.HasValue, str, "")`, the answer is still just `If(str,"")` which is what the various answers on that question all say. – Damien_The_Unbeliever Jul 18 '18 at 13:53
  • No my requirement is to check if a string "" is passed as optional paramer to a function or nothing is passed. And because of this misconception I gained so much downvotes and a duplicate mark. It was this misleading example in my question... I corrected it. – anefeletos Jul 18 '18 at 14:00
  • No the 2 answers in this question (my answer and jmcilhinney's answer) do not say about If(). Both answers (my own included) say about String.Empty vs Nothing. Please remove the duplicate mark because this question is usefull for many people that do not know that `mystring is nothing` can discriminate these whereas `mystring = nothing` can not – anefeletos Jul 18 '18 at 14:08
  • Well, please edit your question to include a better *motivating example*, since you were originally basically asking for the VB equivalent of `??` and even following several edits, your motivating example is given as wanting to write `If(str.HasValue, str, "")`. – Damien_The_Unbeliever Jul 18 '18 at 14:11
  • You are right. there is no need of this. I corrected it. – anefeletos Jul 18 '18 at 14:14

2 Answers2

2

The HasValue property is for nullable value types. For reference types (String is a reference type, as are all classes) you simply compare to Nothing:

If myString Is Nothing Then

Note the use of the Is operator. That is for reference equality, while the = operator is for value equality. Most types only support one or the other but String is one of the few types that support both because they both make sense. Try this to see how they each behave:

Dim nullString As String = Nothing
Dim emptyString As String = String.Empty

If nullString Is Nothing Then
    Console.WriteLine("nullString Is Nothing")
End If

If nullString = Nothing Then
    Console.WriteLine("nullString = Nothing")
End If

If nullString Is String.Empty Then
    Console.WriteLine("nullString Is String.Empty")
End If

If nullString = String.Empty Then
    Console.WriteLine("nullString = String.Empty")
End If

If emptyString Is Nothing Then
    Console.WriteLine("emptyString Is Nothing")
End If

If emptyString = Nothing Then
    Console.WriteLine("emptyString = Nothing")
End If

If emptyString Is String.Empty Then
    Console.WriteLine("emptyString Is String.Empty")
End If

If emptyString = String.Empty Then
    Console.WriteLine("emptyString = String.Empty")
End If

Reference equality checks whether two references refer to the same object, while value equality checks whether two values are equivalent, regardless of what object they are. Nothing and String.Empty are not the same thing in the context of reference equality because one is an object and one is no object, but they are considered equivalent in the context of value equality.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
-2

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%)

anefeletos
  • 672
  • 7
  • 19