Can someone explain something to me?

Hi All.

Have a question, as I don’t understand why this code works:

var preferencesSettings as FolderItem = SpecialFolder.Documents.Child (“screensaverPreferences.txt”)
'var createThePreferencesFile as TextOutputStream
var writeToThePreferencesFile as TextOutputStream

// check if the file exists
if preferencesSettings.Exists = FALSE then // the file DOESN'T exist?
  
  // for testing only
  MessageBox "I will create the TEXTFILE for the movies now. It is called: screensaverPreferencet.txt”
  
  // now create the txt file in the folder theMovies
  writeToThePreferencesFile = TextOutputStream.Create(preferencesSettings)
  writeToThePreferencesFile.Close
  
  writeToThePreferencesFile = TextOutputStream.Open(preferencesSettings)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupMenuStartTimeForScreensaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupmenuEndTimeForScreenSaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.labelVolumeSettingLevel.text)
  
  writeToThePreferencesFile.close
  
else
  
  // it does exist, we will read it in
  MessageBox "The preferences file screensaverPreferences.txt exists”
  writeToThePreferencesFile = TextOutputStream.Create(preferencesSettings)
  writeToThePreferencesFile.close
  
  writeToThePreferencesFile = TextOutputStream.Open(preferencesSettings)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupMenuStartTimeForScreensaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupmenuEndTimeForScreenSaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.labelVolumeSettingLevel.text)
  
  writeToThePreferencesFile.close
end if

If in the section of the if statement where there is NO file, and I create it, and in the section of the code where the file exists and I need to recreate it, if I do NOT do a writeToThePreferencesFile.close and then writeToThePreferencesFile = TextOutputStream.Open(preferenceSettings.txt) the file is created I get an error 0

Why?

Is there a better way to do this?

Regards

You get an error because the tile is already Open and technically “in use”. When you do the “create” you don’t need to open it afterwards.

1 Like

Perhaps simpler and more readable would be:

if preferencesSettings.Exists = FALSE then
  // code here to create it
else
  // code here to open it
end if

// followed by:

// one set of code here to write to the file

writeToThePreferencesFile.close
1 Like

Hi Tim and Greg.

Your solution “kind of” works.

But I run into two problems.

First, if I do what you suggested this way


if preferencesSettings.Exists = FALSE then // the file DOESN'T exist?
  
  // for testing only
  MessageBox "I will create the TEXTFILE for the movies now. It is called: screensaverPreferencet.txt”
  
  // now create the txt file in the folder theMovies
  writeToThePreferencesFile = TextOutputStream.Create(preferencesSettings)
  
else
  
  // it does exist, we will read it in
  //MessageBox "The preferences file screensaverPreferences.txt exists”
  writeToThePreferencesFile = TextOutputStream.Open(preferencesSettings)
  
end if

writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupMenuStartTimeForScreensaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.popupmenuEndTimeForScreenSaver.SelectedRowText)
  writeToThePreferencesFile.WriteLine (windowMyMovieScreenSaver.volumeSlider.Value.ToString)

writeToThePreferencesFile.close

There is a delay in the moviePlayer starting. A couple of seconds (5 maybe). The movie is Playing, but I don’t see those seconds

Secondly, the preferences file doesn’t delete the old settings if the user chooses new values.

Once upon a time, I remember a DeleteLine command, but that might be wishful thinking too.

Regards

Since your design is to re-write the entire preferences file each time, you don’t need to do anything different if the file exists. You should always use Create.

3 Likes

Which I just finished doing and trying. :-D.
I learn so much talking with you folks!

Now to continue with my development.

Any other questions, I’ll ask.

Regards