Monday, September 28, 2009

Working With Text Files in Visual Basic

Visual Basic can be an effective medium used to track calculations in a session of work, however the variables, objects, strings of text, numbers, etc. are volatile; anything created or calculated will be lost when the program is closed. A few of the apps I developed make use of saving certain pieces of information to a small text file before the program is closed, and retrieving the information and putting those pieces back into the app when the program is loaded later.

These text file handling sessions are basically simple enough, but can get a little more complicated when specific logic is required - I'll get into that later.

First off, open Notepad or another light text editor and save a new file. I like to change the extension from .txt to something that is unique to my project - for instance .jus, or .anp, etc. The suffix can be anything; I like to change this from .txt so it is less likely that the file will be opened or its contents changed. Restructuring the contents may cause errors when it is loaded by the application. The .txt suffix can also be used as an alternative, however.

ArcNotepad.anp is the newly created text file

To save a text file with a unique suffix, change the "Save
as type" to "All files" and it may be necessary to put quotes around the file name from the Save As dialog box:


Save text to a file:

Private Sub Save_Click()

Open "C:\ArcNotepad.anp" For Output As #1
    Print #1, "1st line of the document"
    Print #1, "2nd line" 'Line 2, etc.
Close #1

End Sub



Retrieve text from a file:

Private Sub Load_Click()

Dim strLine1 As Integer
Dim strLine2 As Integer

Open "C:\ArcNotepad.anp" For Input As #2
    Line Input #2, strLine1
    Line Input #2, strLine2 'etc.
Close #2

End Sub


That's it for the basics, but let's insert some logic into this. The previous two examples will work if your formatting will be standard - that is, if the text file holds a set number of values/variables that will be updated over and over again, but the number of values will not change. An alternative situation is when lines are added or removed from the text document. The application will not know how many lines are included in the text file, and this is poses a problem. This dynamic text file requires some additional coding to be handled properly. The next time the document is accessed, there will be either:
  • lines that are overlooked (more lines in the text file exist than the previous code looks to retrieve), or
  • the program will return a "Run-time error '62': Input past end of file" error (there are too few lines available for the program to retrieve).
As an example, consider that the two lines "Another line" and "Even more" are added to the text file and saved, for a total of four lines of text. The application will need to read through the file and count how many lines there are. Next, a dynamic array will be created and populated based off of the primary count.

Private Sub Load_Click()

Dim i As Integer

Dim blank As String


'Count the number of lines in ArcNotepad.anp
Open
"C:\ArcNotepad.anp" For Input As #2
    Do While Not EOF(2)
        Line Input #2, blank
        i = i + 1
    Loop Close #2

'Break: Show number of lines in ArcNotepad.anp
MsgBox "Lines: " & i
Dim ii As Integer
'Create a dynamic array, of size i
Dim strLine() As String

ReDim
strLine(i)

'Populate the array with lines from ArcNotepad.anp
Open "C:\ArcNotepad.anp" For Input As #1
    Do While Not EOF(1)
    Line Input #1, strLine(ii)
    ii = ii + 1
    Loop Close #1

'Report the array via message box
Dim iii As Integer
Dim strMessage As String

For iii = 0 To i - 1
    strMessage = strMessage & _
    "Line " & iii + 1 & ": " & strLine(iii) & vbCrLf
Next iii

MsgBox strMessage

Thursday, September 17, 2009

VB / ArcObjects Cheat Sheet

I've been discussing code a bit, and for the non-coder it will make little sense. ArcGIS, along with many other Microsoft-based software packages can be easily customized, and a lot of really cool (nerd) tools and applications can be created with only a little bit of training.

There are many free resources on the internets that explain in great detail how to begin coding. I started learning Visual Basic last summer and have already released an ArcGIS toolset used by the Florida Department of Environmental Protection.

During that time, I saved a few bits of information that I found important to remember, so I threw together this cluttered
PDF file as an ArcObjects Cheat Sheet (VB Reference Sheet):

- Download the VB / ArcObjects Cheat Sheet -

Further Resources:

Thursday, September 3, 2009

Tetris is Aware


I am almost certain that Tetris software is not only cognizant, but I swear that it's also sentient. There are many times when sequences of pieces cannot possibly be part of any random combination
  • Every once in a while it'll throw me a bone and send a few good pieces my way when I get out of a particularly difficult situation
  • It teases me after level 12 by sending groups of I-pieces () - almost to say, "Ha! Bet you didn't expect these NOW!"
  • It knows it's time for bed and starts sending ridiculous sequences of only S and Z pieces ( ) when I've been waiting patiently for an I and there are no good locations for such pieces
I am also beginning to suffer from the Tetris Effect - where one begins to see shapes and attempt to solve lines, away from the game. The first time I had this happen was when I played a lot of competitive The Next Tetris with some friends back in high school.