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
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
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
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]
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)
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)
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=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 ).