Difference between .HasValue and empty string

Difference between .HasValue and empty string

Anonymous
Not applicable
2,121 Views
4 Replies
Message 1 of 5

Difference between .HasValue and empty string

Anonymous
Not applicable

Hi everyone, what is the difference between !el.LookupParameter("Company").HasValue and el.LookupParameter("Company").ToString() != "" ? 

 

foreach (Element el in _AllElements)
{
if (!el.LookupParameter("Company").HasValue || el.LookupParameter("Company").ToString() != "" )
{
//code1
}
}

 

Why if I have only !el.LookupParameter("Company").HasValue the debugger just skips the execution of code1 but if I have both conditions that's when it actually finds unfilled parameter "Company" and executes everything inside of if statement ?

0 Likes
Accepted solutions (1)
2,122 Views
4 Replies
Replies (4)
Message 2 of 5

sahin.ikbal
Advocate
Advocate

It works very similar to normal string of C#.

Difference between
String s=null;

and 

String s= "";

 

Message 3 of 5

Anonymous
Not applicable

@sahin.ikbal you are comparing  "String s=null" and "!el.LookupParameter("Company").HasValue" ?

in that case the question now is what is the difference between

e.LookupParameter("Company") != null

and

!el.LookupParameter("Company").HasValue

Thank you!

Message 4 of 5

sahin.ikbal
Advocate
Advocate
Accepted solution
e.LookupParameter("Company") != null

Here the whole parameter is null, this will only come once there is no such parameter.

But if the parameter value type is something nullable than that null is found by HasValue.

 

 

Message 5 of 5

RPTHOMAS108
Mentor
Mentor

Parameter.HasValue should be renamed. It appears to indicate a state that only exists prior to a parameter being assigned it's first value i.e. false if never used (would like to know if there are any instances otherwise). I don't know if it was meant to work that way but it is the way it appears to work.

 

Probably it comes from exposing some unmanaged idea that we need to check pointer actually refers to something (even that something being nothing) i.e. memory allocated before using it.

 

So not of much use as far as I can tell and a bit dangerously named (depending on your personal philosophy of the meaning of nothing).