Help with folderitem logic tests (.exists) please

  1. 3 weeks ago

    Arthur G

    Feb 5 Pre-Release Testers

    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?

    @Arthur G Can a folderitem be Not Nil and exists=false?

    Yes... as you would see if you were to read what I posted.

    @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

  2. Dave S

    Feb 5 San Diego, California USA

    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
  3. Arthur G

    Feb 5 Pre-Release Testers

    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?

  4. Dave S

    Feb 5 Answer San Diego, California USA

    @Arthur G Can a folderitem be Not Nil and exists=false?

    Yes... as you would see if you were to read what I posted.

    @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

  5. Arthur G

    Feb 5 Pre-Release Testers

    Thanks for confirming. Grumble

  6. Jeff T

    Feb 5 Midlands of England, Europe

    Grumble?

    I have these lines of code that according to my understanding should work but fail when exists = false

    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)

  7. jim m

    Feb 5 Pre-Release Testers Phoenix, Arizona piDog.com

    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.

  8. Dave S

    Feb 5 San Diego, California USA

    @jim m A folderitem cannot be Nil. Nil means you haven't assigned a Folderitem to the property.

    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)

  9. Scott G

    Feb 5 Pre-Release Testers, Xojo Pro

    An example of what Dave is saying:

    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
  10. jim m

    Feb 5 Pre-Release Testers Phoenix, Arizona piDog.com

    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.

    @Scott G f = SpecialFolder.Home.Child("this directory doesn't exist yet").Child("this directory also doesn't exist yet")

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

  11. Dave S

    Feb 5 San Diego, California USA

    Jim.. .sorry, but you keep contradicting yourself.

    @jim m A folderitem cannot be Nil.

    This is NOT a true statement as shown above

    @jim m Nil means you've either not assigned a value or assigned nil to the value.

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

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

    This IS a true statement

  12. 2 weeks ago

    Michael D

    Feb 7 Pre-Release Testers, Xojo Pro

    @Dave S @jim m Right, exactly what I'm saying. FolderItem.Child() is a function that returns either a folderItem OR Nil.
    This IS a true statement

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

or Sign Up to reply!