0

I am setting an array's elements to Nothing if the same elements in another array is Nothing. (for various reasons I can't clone the array). Both arrays are Object type. However, when elements in the first array Objx(i, j) are equal to 0, the logical statement evaluates to Nothing and sets the second array's element to Nothing. Why is a zero element in an array of Object type evaluating to Nothing?

For i = 1 To NumRecords
  For j = 1 To NumFields
    If objx(i, j) = Nothing Then x(i, j) = Nothing
  Next
Next

Also, there is a need use a missing data code, to test if an array's elements are missing, and therefore would be set to Nothing if missing is true.

Dim MissingDataCode As Object = Nothing
For i = 1 To NumRecords
   For j = 1 To NumFields
      If objx(i, j) Is MissingDataCode Then x(i, j) = Nothing
      'If objx(i, j) = MissingDataCode Then x(i, j) = Nothing (does not work)
   Next
Next

Now, if a user needs the missing data code to be set to -9999, and therefore sets:

MissingDataCode = -9999

will the following be able to catch values of -9999 and set the second array to Nothing?

If objx(i, j) = MissingDataCode Then x(i, j) = Nothing

or should the line of code be

If objx(i, j) Is MissingDataCode Then x(i, j) = Nothing
  • What are the array types? The core types like integer use Nothing as the default value (0). For instance, `Dim arryx(1, 1) As Int32` will create an array with all elements 0 (the default). – Ňɏssa Pøngjǣrdenlarp Jul 10 '16 at 17:08
  • 2
    Turn Option Strict On and change comparison to If objx(i, j) Is Nothing Then x(i, j) = Nothing – dbasnett Jul 10 '16 at 17:11
  • They're both Object arrays. –  Jul 10 '16 at 17:11
  • That resolved the issue. –  Jul 10 '16 at 17:13
  • IMHO it's a pretty bad idea to represent a missing value by a value ; what if that value is chosen badly and exists in the array ? you should represent a missing value by ... a missing value and use a nullable integer set to Nothing when you want to signify a missing value because even treated as an array of object the `Is Nothing` test would work as expected – Sehnsucht Jul 11 '16 at 08:12

2 Answers2

0

SOLUTION: if a variable of type Object is employed to catch values of a missing data code, then two if statements need to be used:

Dim MissDataCode As Object = Nothing

If objx(i, j) = MissDataCode And MissDataCode IsNot Nothing Then x(i, j) = Nothing
If objx(i, j) Is MissDataCode And MissDataCode Is Nothing Then x(i, j) = Nothing

This will prevent a zero in the objx(i, j) array from becoming set to nothing. However, if MissDataCode = 0, then anytime objx(i, j) = 0, the result will evaluate to missing and set the second array element x(i, j) to Nothing.

The above two lines will work for any cases of, e.g.:

Dim MissDataCode As Object = Nothing
Dim MissDataCode As Object = -9999
Dim MissDataCode As Object = "NA"
Dim MissDataCode As Object = 0
Dim MissDataCode As Object = ""

Note, there will be occasions when an input zero implies that data are missing.

0

Never use = Nothing for comparison, and use Is Nothing instead. Here are few examples:

Dim o As Object
Console.WriteLine(o = Nothing)  ' True
Console.WriteLine(o Is Nothing) ' True

o = 0
Console.WriteLine(o = Nothing)  ' True
Console.WriteLine(o Is Nothing) ' False

o = ""
Console.WriteLine(o = Nothing)  ' True
Console.WriteLine(o Is Nothing) ' False

Edit My bad .. I didn't notice the second part that was added to the question. Also, you might find this interesting What is the difference between And and AndAlso in VB.NET?

Community
  • 1
  • 1
Slai
  • 22,144
  • 5
  • 45
  • 53
  • If you have Option Strict On ; using `= Nothing` with an `Object` variable is forbidden ; but there is no problem with `= Nothing` for a given type if we understand what that really means – Sehnsucht Jul 10 '16 at 20:17
  • This is true, however, the question involves use of a `MissDataCode`, that when true, sets another array to Nothing. In the SOLUTION provided below if `objx(i, j)=-9998`, then the value is valid and is not a missing number. However, if the user placed -9999 in the data to represent missing, then `objx(i, j) = -9999`, and use of `MissDataCode = -9999` will evaluate to true. In the code above, there is no way to determine that -9998 is not a missing value. –  Jul 11 '16 at 02:16