Writing to a file only once when a boolean toggle is set to True


As can be seen from the image, I have a VB code that writes to a file when the boolean toggle is set to true. My problem is the writing is repeated over and over whilst the toggle value is True. How do I overcome this problem such that it only writes once and then stops without me setting the toggle to off.

Many thanks
William

You’ll need to keep an additional local class level boolean which remembers that you’ve written once. Then when the Export input is False, you set this local boolean to False. When the Export input is True and the local value is False, you write the file and set the local value to True. If the Export input is True and the local value is True, you do nothing.

Would it be possible to send me a bit of coding to show me how to implement this logic. Many thanks.

haven’t written any VB for ages, so this is little more than a hint…

Sub WriteSomething(file As String, contents As String, export As Boolean)
  If (Not export)
    _written = False
    Return
  End If

  If (_written) Then Return

  _written = True
  System.IO.File.WriteAllText(file, contents)
End Sub

Private _written As Boolean

Many thanks David. Same here. I very rarely do any coding other than at the standard level.