

XOJO FOR LOOP CODE
However, during the user interaction with the File Dialog Box it can be the case that the user selected a valid file clicking next the ‘Ok’ button, in whose case our app would get a valid reference but it can also be the case that the user canceled the operation clicking on the ‘Cancel’ button, in which case our variable (‘f’ in the code example) would get a Nil reference. For example: Dim f a FolderItem = GetOpenFolderItem("?") We can also choose to not use any filter at all, passing the string “?” as the function parameter.Īs result of this function invocation we will get a FolderItem instance. When we use this parameter, the File Dialog Box will show only the compatibles ones as selectable by the user, leaving the rest in ghost mode. Choosing Files: GetOpenFolderItemįor these cases Xojo offers the GetOpenFolderItem function, passing along as a unique parameter the string representing, as a filter, the kind of files our app is interested in. How can we get a reference to a file for those cases were we don’t know the file path in advance? I mean, this is what happens, for example, when using most of the apps that allows us to choose the file via a File Dialog Box a Window that brings access to the OS file system showing all the files that are compatibles with the app.

This way we avoid the use of multiple if…then…else in the same code fragment: Exception e as NilObjectException Or, preferably, catching the exceptions as in the following example. It is for things like these that we should always check the correctness of the variable before using any of the provided methods or accessing the properties, because it is possible that we were dealing with a Nil reference.įor example, we can check our FolderItem variables using the conditional form like this: if f Nil then For example, if we were trying to open a file, it could be the case that the file was no longer in the referenced path anymore (moved, deleted…) or maybe we have included a typo in the path composition. In addition, when we use the FolderItem constructor in combination with absolute paths as arguments, we have not guarantees that the resulting reference assigned to the FolderItem variable is a valid one (in our example, pointed by the ‘f’ variable).

For these cases, you can turn to the use of the conditional compilation clause #If…#Then…#Endif, providing the native path variation expected by the OS under which your app is running. It is important to point out the fact that the path format, as String, is the native one to the platform over which we are going to deploy the app, something you have to consider if you want to create a multiplatform app. With the previous line of code we will be using the FolderItem class Constructor, passing along as the argument the native path to the file we want to get the reference to, if it already exists or to the file our app expects to create. In both scenarios, the most basic way to go is the same: Dim f as new FolderItem("\users\myusuario\documents\test.txt") Of course, under iOS and other sandboxed deployments, you will have to consider the limitations imposed by the OS when dealing with files, as for example not writing to the app bundle (under OS X and iOS), accessing only the allowed folders/directories, etc.Īs it goes when using any class, the first thing you will have to do is get a new class instance (an object) pointing to the file item you want to work with, whether it is one already available or one you want to create from scratch (i.e: giving the path, name and extension). Of course, as Xojo is a native multi-platform development environment, the FolderItem class is available for all the supported deployment platforms: desktop (macOS, Windows, Linux, Raspberry Pi), console, web or iOS. The FolderItem class also gives you the methods to do a lot of file operations without effort. Xojo gives you the class FolderItem fully loaded with a useful bunch of properties that allow you to examine the attributes of any file for example, the creation or modification of dates, the file path (in several formats), if the file is an alias, etc. Sooner or later your app will need to work with files, maybe to save the data generated with the app itself, to open the files created with other apps or because is the main purpose of the utility you are working on.
