Help with folderitem logic tests (.exists) please

I changed a folderitem’s tests and am confused. I always understood this second test would fail if the folderitem was not Nil but also did NOT exist.
I have a property that is part of a module and it is a global property.

IniHerd.ThPrefsF

ThPrefsF is the folderitem for the preferences file.
At the moment I am confused because I declared a few hundred lines before

IniHerd.ThPrefsF = IniHerd.ThColl.Child("PrefsFile.ini")

and in this case it doesn’t exist, but is only the location.
I have these lines of code that according to my understanding should work but fail when exists = false

If IniHerd.ThPrefsF <> Nil And IniHerd.ThPrefsF.Exists Then //will succeed if it fulfills both tests ElseIf IniHerd.ThPrefsF = Nil And IniHerd.ThPrefsF.Exists = False Then //Will fail if either is not true End If//If IniHerd.ThPrefsF <> Nil And IniHerd.ThPrefsF.Exists Then

Is my logic correct?

If as folderitem is NIL then it doesn’t exist , so any further testing is a waste
A FolderItem can be NOT NIL and NOT EXISTS, where the Folderitem describes a valid file name/location but that file hasn’t been created yet
A FolderItem can be NOT NIL and EXISTS where the FolderItem is an existing valid file (directory or alias)
The remaining combination NIL and EXISTS is not possible and will never occur

If F=NIL then 
   msgbox "Invalid FolderItem"
else if F.EXISTS=FALSE
   msgbox "Valid File, but it doesn't exist (yet)"
else
msgbox "File is Valid and File Exists"
end if

So just to be sure: Can a folderitem be Not Nil and exists=false?
Or maybe I should make my last test be only exists=false?

Yes… as you would see if you were to read what I posted.[quote=423827:@Dave S]A FolderItem can be NOT NIL and NOT EXISTS, where the Folderitem describes a valid file name/location but that file hasn’t been created yet[/quote]

Thanks for confirming. Grumble

Grumble?

Thats quite confusing code, despite the logic.
Assuming you want to write the file, you can ignore whether it exists or not and just write it.
If you want to append, then yes you need to know if it exists already. But a prefs file is usually rewritten in full.

If IniHerd.ThPrefsF <> Nil then // you have created a badly formed folderitem in memory.. stop! else //folderitem seems OK // create a textoutputstream and write to it end if

if you want to append:

If IniHerd.ThPrefsF <> Nil then // you have created a badly formed folderitem in memory.. stop! else //folderitem seems OK if IniHerd.ThPrefsF.exists then // open a textoutputstream for appending and write to it else // create a textoutputstream and write to it end if end if

This code doesnt contain any exception handlers for inability to write (such as trying to write to a read-only device)

To hopefully further clarify what Dave said…

A folderitem cannot be Nil. Nil means you haven’t assigned a Folderitem to the property.

.Exists checks whether a file or folder exists at the location pointed to by a folderitem.

Checking .Exists on a Nil Object will just raise a NilObjectException.

Jim… that is not entirely correct… it CAN be NIL, if it is invalid , meaning you have assigned a property, but that property cannot define a valid file (not that is does not, but it cannot)

An example of what Dave is saying:

[code]dim f as FolderItem

f = SpecialFolder.Home.Child(“this directory doesn’t exist yet”)

'f is not nil

f = SpecialFolder.Home.Child(“this directory doesn’t exist yet”).Child(“this directory also doesn’t exist yet”)

'f is nil[/code]

Ok, to clarify… Nil means you’ve either not assigned a value or assigned nil to the value.

If it is nil it is not a folderitem. A folderitem is an object. I’m just trying to clarify the difference between the property and the value. Nil means there is no object in the property.

[quote=423835:@Scott Griffitts]f = SpecialFolder.Home.Child(“this directory doesn’t exist yet”).Child(“this directory also doesn’t exist yet”)
[/quote]

Right, exactly what I’m saying. FolderItem.Child() is a function that returns either a folderItem OR Nil.

Jim… .sorry, but you keep contradicting yourself.

This is NOT a true statement as shown above

This is NOT a true statement. As Scott showed above, a VALUE was assigned… it didn’t result in a valid Folderitem

This IS a true statement

[quote=423839:@Dave S]@jim mckay Right, exactly what I’m saying. FolderItem.Child() is a function that returns either a folderItem OR Nil.
This IS a true statement[/quote]

Hmm, I think it’s a little better to say it always returns a FolderItem, but that sometimes the FolderItem it returns is nil.

Side nore: there’s actually an ineresting issue in Xojo which is that doing a copmarison using “= nil” and “is nil” behave differently in some situations. I think that “= nil” will invoke operator_convert but “is nil” will not. (citing this from memory, I may have it mixed up ).