Author Topic: Is Nothing ...  (Read 6211 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Is Nothing ...
« on: December 02, 2015, 01:27:42 AM »
I'm compiling some old VB.net code and came across something I thought was interesting.

The code compiled, netloaded and ran fine , but Resharper's solution-Wide Analysis reports an error
at If rbXD Is Nothing = False Then
      xxxx.vb:29 'Is' expression operand must be reference or nullable type

The changes I made removed the error report.
   
Code - vb.net: [Select]
  1.                     'If btr.IsAnonymous = True Then       'out kdub
  2.                     If btr.IsAnonymous Then               'replaced kdub 2015.12.02
  3.                         Dim rbXD As ResultBuffer = btr.XData
  4.                         rbXD = btr.GetXDataForApplication("AcDbBlockRepBTag")
  5.  
  6.                         'If rbXD Is Nothing = False Then  'out kdub
  7.                         If rbXD IsNot Nothing Then        'replaced kdub 2015.12.02
  8.  
  9.                             For Each tv As TypedValue In rbXD
  10.                                  ' do mojo here
  11.  


The original code would have been written several years ago.
I'm compiling for AC2016 Net Framework 4.6 with VS2015 addin ReSharper 10
Has anyone come across this before ?
« Last Edit: December 02, 2015, 01:48:42 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Is Nothing ...
« Reply #1 on: December 02, 2015, 01:44:08 AM »
Hi,

From MSDN > Visual Basic Reference > Nothing:
Quote
When checking whether a reference (or nullable value type) variable is null, do not use = Nothing or <> Nothing. Always use Is Nothing or IsNot Nothing.

And someones still think VB is simpler than C#...
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is Nothing ...
« Reply #2 on: December 02, 2015, 01:51:52 AM »
Hi,

From MSDN > Visual Basic Reference > Nothing:
Quote
When checking whether a reference (or nullable value type) variable is null, do not use = Nothing or <> Nothing. Always use Is Nothing or IsNot Nothing.

And someones still think VB is simpler than C#...

It sure wasn't me :)


We have a wager here about who will be the first to poke fun at me for using VB.net.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Is Nothing ...
« Reply #3 on: December 02, 2015, 01:54:34 AM »
As VB also stands for VerBose, we should have to write:
Code - vb.net: [Select]
  1. If myOwnVariable IsNot Nothing = True Then
  2.     Return True
  3. End If
:evil:
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Is Nothing ...
« Reply #4 on: December 02, 2015, 08:59:10 AM »
We have a wager here about who will be the first to poke fun at me for using VB.net.



Not me I do not consider VB to be code

tetrahidrocannabinol

  • Guest
Re: Is Nothing ...
« Reply #5 on: December 02, 2015, 03:43:41 PM »
As VB also stands for VerBose, we should have to write:
Code - vb.net: [Select]
  1. If myOwnVariable IsNot Nothing = True Then
  2.     Return True
  3. End If
:evil:


I guess that is a joke, Where do you say that you have read/learned that?

In any case, VB and VB.NET are different things.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is Nothing ...
« Reply #6 on: December 02, 2015, 08:08:01 PM »
Yes it was, I guess he made it up, yes they are different.
I thought it was funny ... painful, but funny.


tetrahidrocannabinol,
after answering 100 posts here and demonstrating that you know what you're talking about you're allowed to make fun of the languages people use to program with. 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Is Nothing ...
« Reply #7 on: December 03, 2015, 03:37:18 AM »
thc,
Yes I was joking.
My apologies if it wasn't explicit, English isn't my native language.
I have not learn this, but I've read similar redundant expressions and not always written in a language of VB family (VB, VBA, VB.NET).
I know VB and VB .NET aren't the same, but I was just joking about language, and, as far as I know VB.NET succeeds to VB and keeps most of its (crappy <- joking?) features.

Anyway, I think any language (or language family) owns its 'culture' and the used language influences the way we are coding, this is essentially due to reproducing the read examples while learning.
For example the use of the prefix 'My' in naming variables is characteristic of the VB(A | .NET) culture, as is a tendency to verbosity.
By my side, I tend to be more verbose when I use C# than when I use LISP or F# (I don't use any language from VB family).

Kerry,
A little ConsoleApplication to illustrate Nothing (strange) behavior:
Code - vb.net: [Select]
  1.     Sub Main()
  2.         Dim i As Integer = 0
  3.         Dim j As Integer = Nothing
  4.         Dim s As String = Nothing
  5.         Dim e As String = ""
  6.         Console.WriteLine("i = Nothing  => {0}", i = Nothing)
  7.         Console.WriteLine("i = 0        => {0}", i = 0)
  8.         Console.WriteLine("j = Nothing  => {0}", i = Nothing)
  9.         Console.WriteLine("j = 0        => {0}", i = 0)
  10.         Console.WriteLine("s = Nothing  => {0}", s = Nothing)
  11.         Console.WriteLine("s Is Nothing => {0}", s Is Nothing)
  12.         Console.WriteLine("s = """"       => {0}", s = "")
  13.         Console.WriteLine("e = Nothing  => {0}", s = Nothing)
  14.         Console.WriteLine("e Is Nothing => {0}", s Is Nothing)
  15.         Console.WriteLine("e = """"       => {0}", s = "")
  16.     End Sub
Output:
Code: [Select]
i = Nothing  => True
i = 0        => True
j = Nothing  => True
j = 0        => True
s = Nothing  => True
s Is Nothing => True
s = ""       => True
e = Nothing  => True
e Is Nothing => True
e = ""       => True

Code - vb.net: [Select]
  1. i Is Nothing
throws a compilation time error because i is a non nullable value type.
« Last Edit: December 03, 2015, 04:37:32 PM by gile »
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is Nothing ...
« Reply #8 on: December 03, 2015, 05:05:39 PM »
< .. >

Anyway, I think any language (or language family) owns its 'culture' and the used language influences the way we are coding, this is essentially due to reproducing the read examples while learning.
For example the use of the prefix 'My' in naming variables is characteristic of the VB(A | .NET) culture, as is a tendency to verbosity.
By my side, I tend to be more verbose when I use C# than when I use LISP or F# (I don't use any language from VB family).

< .. >

I think that is a good observation gile.

and Thanks for the ConsoleApplication printout.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Is Nothing ...
« Reply #9 on: December 04, 2015, 12:18:14 PM »
I won't name names but at an Autodesk University class taught by an Autodesk API member I saw this code.

Code - vb.net: [Select]
  1. If DoSomeMethod(Variable) = True Then
  2.  
  3.     DoSomething
  4.  
  5. End If

While technically not incorrect God does kill a kitten every time someone does this.

** edited code to equal true instead of false **
« Last Edit: December 05, 2015, 01:14:57 AM by Keith Brown »
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Is Nothing ...
« Reply #10 on: December 05, 2015, 12:14:20 AM »
I won't name names but at an Autodesk University class taught by an Autodesk API member I saw this code.

Code - vb.net: [Select]
  1. If DoSomeMethod(Variable) = False Then
  2.  
  3.     DoSomething
  4.  
  5. End If

While technically not incorrect God does kill a kitten every time someone does this.

Are you suggesting that code should only do something if a method returns True .. because if you are, I must admit that I have single handedly caused the death of tens of thousands of poor kittens.

On second thought, maybe the method needs to be refactored to return True so you can only do stuff when it is True ...

Hell, I dunno ... I write code that makes sense to me when I write it, later on I shake my head in wonder .. as in I wonder what the hell I was thinking when I wrote that.

I prefer something like this
Code - vb.net: [Select]
  1. If DoSomeMethod(Variable) Then
  2.     DoSomething
  3. End If

or maybe
Code - vb.net: [Select]
  1. DoSomeMethod(InVariable OutVariable)
  2. If OutVariable = Somevalue Then
  3.     DoSomething
  4. End If

What I don't like is this
Code - vb.net: [Select]
  1. Function MyFunction() As Boolean
  2.     If DoSomeMethod() = True
  3.         Return True
  4.     Else
  5.         Return False
  6.     End If
  7. End Function
  8. 'or this
  9. Function MyFunction(ReferenceObject) As Boolean
  10.     Dim TestObject As Object
  11.     For X As Integer = 0 to 9
  12.         Variable = GetSomeObjectByMethod(X)
  13.         If TestObject Equals ReferenceObject
  14.             Return True
  15.         End If
  16.     Next X
  17.     Return False
  18. End Function
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Is Nothing ...
« Reply #11 on: December 05, 2015, 01:16:17 AM »
Oops.  Edited my post as the conditional check should have been equal to True.   It was a long week and just got home from AU.

So I was trying to suggest you only need this

Code - vb.net: [Select]
  1. If DoSomeMethod(Variable) Then
  2.     DoSomething
  3. End If
« Last Edit: December 05, 2015, 01:20:19 AM by Keith Brown »
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is Nothing ...
« Reply #12 on: December 05, 2015, 01:36:53 AM »

Those last couple of snippets remind me :

I'm not really picking on VB, but is it the only language set that used the same symbol for the assignment operator and the equality test operator ??


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Is Nothing ...
« Reply #13 on: December 05, 2015, 04:05:03 PM »
I honestly don't know ... but I cannot think of another language that does.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Is Nothing ...
« Reply #14 on: December 06, 2015, 05:38:59 PM »
I'm not really picking on VB, but is it the only language set that used the same symbol for the assignment operator and the equality test operator ??
It's not really a language but Excel formulas use a single = for both assignment and equality. Of course, Excel is no stranger to VB(A). Also, Excel allows such abominations as ="string " & "more string "& (100-3)*7 so a single equals sign is not its most offensive behaviour.