Better way

  • Thread starter Thread starter Steve Peterson
  • Start date Start date
S

Steve Peterson

Hi

I'm curious - which is the better ".NET" method to use if needing to know if
an object is Nothing?

1: If IsNothing(myobject) ...

or

2. If myobject Is Nothing ...

TIA
Steve
 
Steve Peterson said:
I'm curious - which is the better ".NET" method to use if needing to know
if an object is Nothing?

1: If IsNothing(myobject) ...

or

2. If myobject Is Nothing ...

I believe the decision should be based on personal preference. I prefer the
latter one.
 
Me too. My favorite part is when I can write:

If Not MyObject is Nothing ...

Take that all you english teachers :P
 
If Not MyObject is Nothing ...

Take that all you english teachers :P

this is cool :-)


but serious now ... i prefer IsNothing(myobject) as it is more clear in my
opinion , but as said by the others it is more a personal preference
maybe because i also program a lot in T-sql where simular syntax is used
( isnull etc etc function calls ) i just automaticly typ this in

regards

Michel Posseth [MCP]
 
TrtnJohn said:
Me too. My favorite part is when I can write:

If Not MyObject is Nothing ...

Take that all you english teachers :P

VB 2005 supports the patented 'IsNot' operator:

\\\
If MyObject IsNot Nothing Then...
///
 
Let's not forget, new to VB 2005:

If MyObject IsNot Nothing

You can also do this:

If Not MyObject IsNot Nothing

What would your english teacher say now?
 
Chris Dunaway said:
Let's not forget, new to VB 2005:

If MyObject IsNot Nothing

You can also do this:

If Not MyObject IsNot Nothing

What would your english teacher say now?

Get back in school! Either that or "Oh, my gawd!"
 
After seeing all the English lessons, I guess I should clarify:

Performance-wise which is better?
Which method is the backward-compatible method to the VB6 days..
Which method should I get in the habit of using to be ".NET" correct?

Steve

PS Just to throw in my 2 cents worth - I have been using the
"IsNothing(myobject)" method.. IN my mind it sounds more "rational". If I
want to negate it it is: If Not IsNothing(myobject).. I dunno - just sounds
more logical...
 
Steve,
In addition to the other comments. Its largely personal preference.

However:
| 1: If IsNothing(myobject) ...

Is two function calls into a library, which may or may not be in-lined by
the JIT compiler.
//000038: If IsNothing(myobject) Then
IL_0003: ldloc.2
IL_0004: call object
[mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
IL_0009: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_000e: stloc.s VB$CG$t_bool$S0
IL_0010: ldloc.s VB$CG$t_bool$S0
IL_0012: brfalse.s IL_0014
//000039:
//000040: End If
IL_0014: nop


While:

| 2. If myobject Is Nothing ...

Is inline IL code.
//000041: If myobject Is Nothing Then
IL_0015: ldloc.2
IL_0016: ldnull
IL_0017: ceq
IL_0019: stloc.s VB$CG$t_bool$S0
IL_001b: ldloc.s VB$CG$t_bool$S0
IL_001d: brfalse.s IL_001f
//000042:
//000043: End If
IL_001f: nop


FWIW: the "patented" IsNot operator also generates IL code. Oddly enough it
generates the same code that C#'s reference inequality operator has been
generating since C# 1.0.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi
|
| I'm curious - which is the better ".NET" method to use if needing to know
if
| an object is Nothing?
|
| 1: If IsNothing(myobject) ...
|
| or
|
| 2. If myobject Is Nothing ...
|
| TIA
| Steve
|
|
 
I should add that the IL code I gave is for the Debug build, the Release
build's IL is optimized slightly. Plus the JIT will optimize the Release
build even more, such as inlining the GetObjectValue and/or the IsNothing
method themselves. So performance wise you really need to profile the
routine & determine if that routine needs to be "micro optimized" by
choosing IsNothing() over Is Nothing.

As I & others stated, its personal preference.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message | Steve,
| In addition to the other comments. Its largely personal preference.
|
| However:
|| 1: If IsNothing(myobject) ...
|
| Is two function calls into a library, which may or may not be in-lined by
| the JIT compiler.
| //000038: If IsNothing(myobject) Then
| IL_0003: ldloc.2
| IL_0004: call object
|
[mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
| IL_0009: call bool
|
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
| IL_000e: stloc.s VB$CG$t_bool$S0
| IL_0010: ldloc.s VB$CG$t_bool$S0
| IL_0012: brfalse.s IL_0014
| //000039:
| //000040: End If
| IL_0014: nop
|
|
| While:
|
|| 2. If myobject Is Nothing ...
|
| Is inline IL code.
| //000041: If myobject Is Nothing Then
| IL_0015: ldloc.2
| IL_0016: ldnull
| IL_0017: ceq
| IL_0019: stloc.s VB$CG$t_bool$S0
| IL_001b: ldloc.s VB$CG$t_bool$S0
| IL_001d: brfalse.s IL_001f
| //000042:
| //000043: End If
| IL_001f: nop
|
|
| FWIW: the "patented" IsNot operator also generates IL code. Oddly enough
it
| generates the same code that C#'s reference inequality operator has been
| generating since C# 1.0.
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| || Hi
||
|| I'm curious - which is the better ".NET" method to use if needing to know
| if
|| an object is Nothing?
||
|| 1: If IsNothing(myobject) ...
||
|| or
||
|| 2. If myobject Is Nothing ...
||
|| TIA
|| Steve
||
||
|
|
 
Chris,

I never understand (beside that it is something more difficult to build for
the ones who have to translate the source to the code), that never is used

If MyObject Is Something

It seems for me that for English speaking people Something means something
different from Not Nothing or IsNot Nothing.

Cor
 
Steve,
Performance-wise which is better?
Which method is the backward-compatible method to the VB6 days..
Which method should I get in the habit of using to be ".NET" correct?

In 2005, for me it is more important that the program is easy to understand
when it has to be maintanance than those (not even realistic) 10 seconds
that we can win with 1000 users in a year. (Not realistic because users have
their natural wait moments).

Backwards compatible is only important for old programs. New programs have
no backward compatible problems, only forward compatible problems, which is
the problem from Microsoft, not ours, they have to keep an eye on that or
write converters.

Every method that can be used in Net is .Net correct.

Just my thought,

Cor
 
In 2005, for me it is more important that the program is easy to
understand
In the year 2005, for ..........

It can be confuse with the Visual Studio version, therefore this message
 
Cor,
| It seems for me that for English speaking people Something means something
| different from Not Nothing or IsNot Nothing.
I don't know, I would have preferred "Something" to be an alias for "IsNot
Nothing", however with the IsNot you can compare two distinct instances.:

Dim obj1 As New ...
Dim obj2 As New ...

If obj1 IsNot obj2 Then
' do this
End If

Personally I would like to see both IsNot & Something.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Chris,
|
| I never understand (beside that it is something more difficult to build
for
| the ones who have to translate the source to the code), that never is used
|
| If MyObject Is Something
|
| It seems for me that for English speaking people Something means something
| different from Not Nothing or IsNot Nothing.
|
| Cor
|
|
 
Jay,

I agree although I tend still more for

If Not obj1 Is obj2 Then

You know probably already that I don't like created not English words in
VB.Net.

One of the reasons, they are not to find for people for who English is daily
not the first language in a (translation) dictionary, and because those are
never complete, we are never sure if the word exist, although I use now
than Internet.

:-)

Cor
 
After seeing all the English lessons, I guess I should clarify:

Performance-wise which is better?

In theory, myObject Is Nothing is slightly faster, since IsNothing
requires a function call. In reality, the function call is likely to get
Jitted away, and even if it didn't you're just not going to notice any
difference.
Which method is the backward-compatible method to the VB6 days..
Which method should I get in the habit of using to be ".NET" correct?

Like others have said, it's personal preference. But "object Is
Nothing" does correspond a bit more closely to the way other .Net
languages tend to handle this construct.

Whether that matters is up to you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top