Visual Basic Code File System Object Text Stream

Visual Basic Code: File. System. Object, Text. Stream Controls: Up. Down

Cookie A Persistence Example w A cookie (sometimes known as a persistence cookie) is a file placed on a user’s computer by a web server that stores information about the user’s having visited and used a web site n It might store various custom settings, which hyperlinks have been clicked, and so on

Persistence w An object (program) is said to have persistence if it stores and recalls data from previous executions w The data is not stored in the program file but in a separate file

Storage The persistence of memory

Object Oriented Files w There is more to a file than just a pointer indicating its location. A file n n n Has a name and location May exist or not exist May be read/write or read only May be in use by another user Etc. w The above contribute to the properties and methods of an object oriented file

File System Object w The first step in accessing a file in VB is to instantiate a new File. System. Object w The File. System. Object contains a whole hierarchy of information about a file, e. g. the drive it’s on, folder it’s in, etc. n One can gather information on, as well as create, delete and change files and folders w (p. 275 and pp. 282 -308 in VB & VBA)

Reference w Strictly speaking the File. System. Object is not a part of VB but of the Scripting Runtime library, therefore one needs to reference the Scripting Runtime library n n A reference is a way to expand VB’s namespace – that is, introduce new “key words” See View, Object Browser w Go to Project/References w Scroll down and select (check) Microsoft Scripting Runtime

Simple text files w The File. System. Object allows one to deal with a file as a simple text (Strings) file, e. g. n n Read a line, read the next line, and so on Write a line, write the next line, and so on w The ultimate passing strings back and forth (reading and writing) is done by the Text. Stream Object w (pp. 550 -555 in VB & VBA)

Other files w Random-access files (which allow a nonsequential access to the data) and application files like Word’s doc and Excel’s xls (which contain formatting information in addition to data) are accessed in a different way

Ignoring the middle ground w In between the File. System. Object and the Text. Stream are the Folder Objects and the File Objects w If one does not need specific information about the folders and files but simple wants to read and write, one can go directly from File. System. Object to Text. Stream

Declare vs Instantiate w Dim ts. Stream. Message as Text. Stream n Declares a “pointer” that can point to a Text. Stream object, the object (methods and properties associated with Text. Stream are not copied to memory, yet. w Dim fso as new File. System. Object n This declares and instantiates Dim o as Some. Object ‘declare … Set o = new Some. Object ‘instantiate

Example: Writing to a text file

Example: Writing to a text file

Example: Writing to a text file Option Explicit Dim fso. Message. File As New File. System. Object ‘declare and instantiate Dim ts. Text. Message As Text. Stream ‘declare Dim Message As String Path and file name Private Sub Form_Load() Dim file. Name as String file. Name = App. Path & "message. txt", Set ts. Text. Message = fso. Message. File. Open. Text. File( file. Name, For. Writing, True) End Sub Private Sub cmd. Send_Click() Read, Message = txt. Message. Text ts. Text. Message. Write (Message) End Sub write or append Create if it doesn’t exist

Writing versus Appending w In the previous program the Text. Stream was created for writing, if the program is run again, causing the file to be reopened, the second message overwrites the first w Note that this is different from writing more before the program ends (the file was only opened once)

Writing a second time

Writing a second time

Appending Instead

Appending Instead Option Explicit Dim fso. Message. File As New File. System. Object Dim ts. Text. Message As Text. Stream Dim Message As String Private Sub Form_Load() Dim file. Name as String file. Name = App. Path & "message. txt" Set ts. Text. Message = fso. Message. File. Open. Text. File(file. Name, for. Appending, True) End Sub Still use write here Private Sub cmd. Send_Click() Message = txt. Message. Text ts. Text. Message. Write (Message) End Sub

Example: Reading from a file

Example: Reading from a file

Example: Reading from a file Option Explicit Dim fso. File. To. Read As New File. System. Object Dim fo. File. To. Read As File Dim ts. Message. Read As Text. Stream Private Sub Form_Load() Set fo. File. To. Read = fso. File. To. Read. Get. File(App. Path & _ "message 2. txt") Set ts. Message. Read = fo. File. To. Read. Open. As. Text. Stream End Sub ‘going through File object instead of directly to Text. Stream

Reading (Cont. ) Private Sub cmd. Read_Click() txt. Message. Text = ts. Message. Read. All End Sub Reads entire contents of file at once

Text to be read line-by-line

Reading line-by-line

Reaching the end

Reading line-by-line (part 1) Option Explicit Dim fso. My. File As New File. System. Object Dim fo. My. File As File Dim ts. My. Text As Text. Stream Private Sub Form_Load() Set fo. My. File = fso. My. File. Get. File(App. Path & "message 3. txt") Set ts. My. Text = fo. My. File. Open. As. Text. Stream End Sub

Reading line-by-line (part 2) Private Sub cmd. Next_Click() If Not ts. My. Text. At. End. Of. Stream Then txt. Message. Text = ts. My. Text. Read. Line Else txt. Message. Text = "THE END" cmd. Next. Enabled = False End If End Sub Asks whether the end has been reached

Modal vs Modeless Forms w If two forms are showing at once, can the user switch from one to the other? w Yes, if the newer form to be shown is “modeless” (the default) w No, if the newer form is “modal” w frm. New. Show 1

Modal Forms

Modal Form (Code) Option Explicit Private Sub Command 1_Click() frm. Form 2. Show 1 ‘frm. Form 2. Show vb. Modal End Sub

Code module w When code is not directly associated with a form, it can be placed in a code module w Project/Add Module w By making code-module functions and/or subroutines Public they become available to other modules (forms) n reuse

Code module Code Option Explicit Public Function Compute. Average(Number() As Integer) As Double Dim i As Integer Compute. Average = 0 For i = LBound(Number) To UBound(Number) Compute. Average = Compute. Average + Number(i) Next i Compute. Average = Compute. Average / (UBound(Number) - _ LBound(Number) + 1) End Function

Example Code Module

Form calling a function in code module Option Explicit Private Sub cmd. Average_Click() Dim Average As Single Dim Grades(2) As Integer Dim i As Integer For i = 0 To UBound(Grades) Grades(i) = CInt(txt. Grade(i). Text) Next I Average = Compute. Average(Grades) ‘call to code module txt. Answer. Text = Average End Sub

Type Casting w A. k. a. Type Conversion w c. Lng(variable) if variable can be converted into a long, it does so w Data coming from Text. Boxes and Text. Streams is in the form of strings, it may have to be converted so the programs “knows” that it’s a number w (p. 31 in VB & VBA)

Adding Text. Boxes w txt. Answer. Text=txt. Number 1. Text + _ txt. Number 2. Text n String concatenation w txt. Answer. Text=Cint(txt. Number 1. Text) + _ Cint(txt. Number 2. Text) n Adds numbers (provided numbers where in the textboxes)

Up. Down Component w Project/Components (or right click on the toolbox (where controls are)) w Microsoft Windows Common Controls 2. 0 w (a control formerly known as spinner) w Used to increment/decrement an associated (buddy) control w (p. 366 in Visual Basic Controls)

Up. Down Properties w Buddy. Control: the associated control that the Up. Down control affects w Buddy. Property: the property of the associated control that is affected w Value: the number being incremented/decremented w Min: the lowest value possible w Max: the highest value possible w Increment: the amount changed by on a single click

Password. Char Property w Text. Boxes have a Password. Char Property w If the Password. Char property is set to a character (e. g. *) then that character appears for every printable character entered in the textbox w The Text property of the Text. Box holds the actual characters enetered

Searching Do While Not ts. Password. At. End. Of. Stream User. Line = ts. Password. Read. Line … tokenize line into username and password If UCase(Username) =UCase(txt. Username. Text)_ And Password = txt. Password. Text Then … do whatever Exit Do ‘once found stop searching End If Loop …why here? found or not?
- Slides: 41