Testing for Nil

Looks like I’m starting out in the ditch this morning. What might be wrong with testing for nil in this code. Test array is a property TestArry(10)

var day as integer

For day = 1 To 10
If TestArray(day) = Nil Then
// do something
End
Next

What type is TestArray()?

Integer. The error is :
Type mismatch error. Expected Integer, but got Nil
'If TestArray(day) = Nil Then

Integer won’t / can’t be nil.

Are you pre-populating this array? If so then use -1 as a (nil as the original intended target) so you can test for -1 instead of NIL.

Something like this?

    Var testArray() as Integer = Array(0,0,2,3,4,5,6,7,8,9)
    var day as integer

    For day = 0 to TestArray.LastRowIndex
      
      Var thisEntryInt as Integer = TestArray(day)
      
      If thisEntryInt <> 0 Then
        Txt_Output_Stream.WriteLine “0”
      Else
        Txt_Output_Stream.WriteLine Str(RainSensorArray(day))
      End

    Next

NIL is a state that an object (class) when you instantiate it.
https://documentation.xojo.com/api/language/nil.html

HTH,
Mike

1 Like

Or make it a Variant array where the elements can be Nil.

(But I like Mike’s suggestion better if there is some value you can test against that would adequately represent “nil”.)

2 Likes

Interesting.

You read my mind. I wanted to save 365 elements in a text file which are rain totals for each day. Then I could associate the position in the array with a day of year. Some days would be blank. This was my thinking which can often be flawed:

For day = 1 To 365
If RainSensorArray(day)= Nil Then
Txt_Output_Stream.WriteLine “0”
Else
Txt_Output_Stream.WriteLine Str(RainSensorArray(day))
End
Next

Kim, your Idea was the easy fix for my situation. Both thoughts mentioned were important to understand the error I was getting. Thanks

1 Like

The issue will be for you that if it’s a blank string or something it will be converted to an integer of 0.

Is the original source of your data a String or integer 100% end to end?

Arguably, shouldn’t a day with no value actually have “0” rainfall? That seems to make sense to me.

Yes, and that is why I’m setting the element to “0” if no value is found.

1 Like

Excellent then! I also updated my example above to match that.

I think there would be a flaw with that code. I do not wish to overwrite any good values in the array with “0” since I save/update the array every day to the HD. It needs to maintain all existing values. I need to test it yet but changing the array to a variant may be answer for my code.

1 Like

Just set to -1 if not valid – Then you will be solid unless that is an issue then the Variant route is solid also.

Thanks Mike, it’s all good to help with the steep learning curve I’m on.

…and do not forget the leap year… day !

Trouble maker! :face_with_raised_eyebrow:

Convenient :slight_smile:

Actually, I don’t think it’s an issue since the array will be 366 days and a new array is only triggered when the system clock changes the year. I am only interested in daily totals to override my watering system and a yearly total for grins. Not a 366th day, who cares it’s “0”

No, experienced oldster who forgot things everytime :wink:

Now, think at the Jewish Calendar (new year occured to them (Roch Hachana ended yesterday) and at the muslim calendar. I do not even talk about the different dates in use in India.

That would be trouble making :wink: