Posted By: Red5 | Jun 27th @ 11:04 AM
page 1 of 2
Comments: 32 | Views: 1411
Red5
Red5
Systems Manager Curmudgen

Is this supposed to evaluate to TRUE in vb.net?
--------------------
Dim strMessage as String

If strMessage = "" Then
    'if true, do some stuff here
End If
-------------------

strMessage, in debug mode, shows a value of "Nothing".
Is "Nothing" supposed to equal a string of zero length?
I would have thought Nothing == Null.....which is different than ""

stevo_
stevo_
Casablanca != Manchester
VB will use its own library to do string comparisons, of which is will essentially say that, null is nothing, zero length is nothing. If you pull the IL apart, you'll find VB calling a visual basic compiler services method of some sort.

Kinda one of the reasons I don't like VB, brings a ton of crap with it (althought I think I heard you can tell the compiler not to do this now).
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
Under C# this would definitely not be the case - (String.Empty != null). There is a static method on the string class that provides simmilar functionality:

static bool String.IsNullOrEmpty(String str);
stevo_
stevo_
Casablanca != Manchester

I appreciate the addition, but that really makes me sound like I've been doing .NET for 3 days or something Tongue Out

ZippyV
ZippyV
Soapbox = Fail
Kinda one of the reasons I don't like VB, brings a ton of crap with it (althought I think I heard you can tell the compiler not to do this now).

Option Strict On ?
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
Anything you read into my comment further than the strictly technical details I provided was entirely unintentional.
stevo_
stevo_
Casablanca != Manchester
No, its runtime agility:

http://community.bartdesmet.net/blogs/bart/archive/2007/09/03/visual-basic-9-0-feature-focus-runtime-agility.aspx

But its more if you drastically need compilation control I guess.. plus- it doesn't stop vb from trying to call that method.. but it does mean you get to define the method yourself.
littleguru
littleguru
allein, allein,... allein, allein!
You could probably use:

String.Compare(strMessage, "")

to walk around the "problem" of VB.NET. This will fall back to the bahaviour that you expect.
At least in VB6 "" a.k.a. vbNullString <> Nothing, so I doubt they'd change it VB.NET.  In fact I think it would give a compiler error in VB6, since a String isn't an object and only objects can be compared with Nothing.

I just tried this in VS2008 and it does evaluate to true....wow...does 0 equal nothing too, since that's the default value of an Integer....wow that does too....who made that decision?
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
Option Strict has no bearing on the problem.

And yes, VB considers Nothing and "" to be equivalent in string comparisons. It has done so since the first version of VB.NET (can't remember how VB6 behaved). I find it a very useful behaviour as writing foo = "" is easier than String.IsNullOrEmpty(foo), which has the same effect.

If you must make the distinction, use String.Compare as Littleguru suggested.
littleguru
littleguru
allein, allein,... allein, allein!
You also might to want Equals...

String.Equals(strMessage, String.Empty)

if you like that more. It also returns a Boolean instead of an Integer and it's also what the == operator on String uses internally.
strMessage Is String.Empty also works.
Well, in VB.NET Nothing has a very different meaning than Null in C#.  In C#, null refers only to reference types and means there is no reference assigned to the reference variable.  Nothing, on the other hand is the default value of any particular data type or object.

Note that  String.IsNullOrEmpty is much faster then == "". If your in a loop (for example, when parsing a file) you can defiantly see the difference.

See: http://msdn.microsoft.com/en-us/ms182279.aspx

evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
And .Length == 0 is even faster if you know that the string is non-null. In fact:

static bool IsNullorEmpty(string s){
  return ((object)s == null) || (s.Length == 0);
}
stevo_
stevo_
Casablanca != Manchester
I think that is inferred by the example between 1.1 and 2.0 Tongue Out but yes.. any real world performance charts on it?
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
No, but looking at the .NET source code (or using reflector) will show you that my code is identical to the method call, but without either

a) The method call
b) A comparison

So we're talking in the region of 12-25 CPU cycles (depending on the size of the instruction branching lookahead buffer on the CPU) per call.

Which adds up.
Maybe a stupid question, but can I ask why you cast to an object before comparing to null? Why not just:
return s == null || s.Lenght == 0;

Doesn't the cast cost performance?
stevo_
stevo_
Casablanca != Manchester
Theres no reason to cast the string.. it could be a legacy thing he's used to.. and yes the IL will contain a castclass opcode for the downcast, so it will cost performance.
TommyCarlier
TommyCarlier
Trust me, I'm from the Internets
That would be true, if String.IsNullOrEmpty was implemented like that. But it's not. Reflector shows us that String.IsNullOrEmpty is implemented like this in IL:
.method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: brfalse.s L_000d
    L_0003: ldarg.0 
    L_0004: callvirt instance int32 System.String::get_Length()
    L_0009: ldc.i4.0 
    L_000a: ceq 
    L_000c: ret 
    L_000d: ldc.i4.1 
    L_000e: ret 
}
This translates into the following C# code:
public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}
evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?