1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.

Slides:



Advertisements
Similar presentations
Practical Programming COMP153-08S Lecture: Repetition Continued.
Advertisements

IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Arrays & Strings part 2 More sophisticated algorithms, including sorting.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Additional loop presentation
Chapter 8 - VB.Net by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files Creating a Sequential File Adding Items to a Sequential File Error Trapping.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 9 Files I/O: Files, Records and Fields Part 3.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 8 - VB 2008 by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files 8.2 Using Sequential Files.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Creating Sequential Files, Adding Data, & Deleting Data.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Tutorial 6 The Repetition Structure
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Understanding Data Types and Collections Lesson 2.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
Click to edit Master text styles Stacks Data Structure.
Computer Science Up Down Controls, Decisions and Random Numbers.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
Visual Basic Fundamental Concepts
Objective 7.04 Apply Built-in String Functions (3%)
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Files and Streams.
CS313D: Advanced Programming Language
Visual Basic..
Variable Review & IO User 12/26/2018.
Chapter 3.5 Input and Output
Input and Output.
Chapter 6 - VB.Net by Schneider
CIS16 Application Development and Programming using Visual Basic.net
Basic Collections.
Input and Output.
Files and Streams.
Input and Output Chapter 3.5
Presentation transcript:

1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E

2 COMP3100e Objectives  Q&A  Homework Assignment  Homework Solution  Sequential Files

3 COMP3100e Collections  A collection is a set of similarly typed objects that are grouped together.  Objects of any type can be grouped into a single collection of the type Object to take advantage of constructs that are inherent in the language. For example, for each in Visual Basic expects all objects in the collection to be of a single type.

4 COMP3100e  System.Collections classes can generally be categorized into three types: –Commonly used collections. »These are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and lists. Commonly used collections have generic versions and nongeneric versions. –Bit collections. »These are collections whose elements are bit flags. They behave slightly differently from other collections. –Specialized collections. »These are collections with highly specific purposes, usually to handle a specific type of element, such as StringDictionary.  Be sure to choose a collection class carefully. Because each collection has its own functionality, each also has its own limitations. The more specialized a collection is, the more limited it is.

5 COMP3100e System.Collection s ArrayList Implements the IList interface using an array whose size is dynamically increased as required. BitArray Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). CaseInsensitiveComparer Compares two objects for equivalence, ignoring the case of strings. CaseInsensitiveHashCodeProvider Supplies a hash code for an object, using a hashing algorithm that ignores the case of strings. CollectionBase Provides the abstract base class for a strongly typed collection. Comparer Compares two objects for equivalence, where string comparisons are case-sensitive. DictionaryBase Provides the abstract base class for a strongly typed collection of key/value pairs. Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key. Queue Represents a first-in, first-out collection of objects. ReadOnlyCollectionBase Provides the abstract base class for a strongly typed non-generic read-only collection. SortedList Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. Stack Represents a simple last-in-first-out (LIFO) non-generic collection of objects

6 COMP3100e Collections Public Class Form1 'Declare a variable of type Control to represent form controls Dim ctrl As Control Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each ctrl In Controls ctrl.Text = "Click Me!" Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click For Each ctrl In Controls ctrl.Left = ctrl.Left + 25 Next End Sub Private Sub btnMoveObjects_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveObjects.Click For Each ctrl In Controls If ctrl.Name <> "btnMoveObjects" Then ctrl.Left = ctrl.Left + 25 End If Next End Sub End Class

7 COMP3100e Collections (Continued) Property CountRead-only. Returns an Integer containing the number of elements in a Visual Basic collection. ItemRead-only. Returns a specific element of a Visual Basic Collection object either by position or by key. Method AddAdds an element to a Visual Basic Collection object. ClearRemoves all elements from a Visual Basic Collection object. Contains (Collection Object) Returns a Boolean indicating whether a Visual Basic Collection object contains an element with a specific key. GetEnumerator Returns an enumerator object for iterating over the collection. RemoveRemoves an element from a Visual Basic Collection object.

8 COMP3100e Basics of SF  Choose a name for the file  Choose a name to be used to refer to the file  open the file Dim sw As IO.StreamWriter = _ IO.File.CreateText("PIONEERS.TXT") Dim sr As IO.StreamReader = _ IO.File.OpenText("PIONEERS.TXT")  Interact with the file sr.ReadLine sw.WriteLine("Atanasoff“)  close the file –sr.Close() –sw.Close()

9 COMP3100e Basics Continued  The.NET System.IO namespace includes a class library that facilitates string, character, and file manipulation.  These classes contain properties, methods, and events for creating, copying, moving, and deleting files. − As both strings and numeric data types are supported, they also allow you to incorporate data types in files.  The most commonly used classes are: − FileStream − BinaryReader − BinaryWriter − StreamReader − StreamWriter

10 COMP3100e Basics Continued  If file is opened for output and it already exists, the existing file is erased.  If a file is opened for input and it doesn’t exist, the program is forever cursed and it’s programmer is delegated to writing lesser spells. –If IO.File.Exists(Filename) is true, then the file exists  The method “Peek” is commonly used to determine when the end of file is encountered (equal to a -1)

11 COMP3100e The Three Modes  CreateText Dim sw As IO.StreamWriter = _ IO.File.CreateText("PIONEERS.TXT")  OpenText Dim sr As IO.StreamReader = _ IO.File.OpenText("PIONEERS.TXT") Dim sr As StreamReader ' Open the file to read. Sr = File.OpenText("c:\MyFile.txt")  AppendText  Dim sw As IO.StreamWriter = _ IO.File.OpenText("PIONEERS.TXT")

12 COMP3100e Simplified IO  After OPTION Strict ON, insert –Imports System.IO –Then you don’t have to say IO. All the time…  File.Delete (filename)  File.Move(oldname, newname)

13 COMP3100e Output example 'Create the file PIONEERS.TXT Dim sw As IO.StreamWriter = _ IO.File.CreateText("PIONEERS.TXT") With sw.WriteLine("Atanasoff").WriteLine("Babbage").WriteLine("Codd").WriteLine("Dijkstra").WriteLine("Eckert").WriteLine("Faggin").WriteLine("Gates").WriteLine("Hollerith").Close() End With

14 COMP3100e Input Example 'Display the contents of the file ' PIONEERS.TXT in a list box Dim sr As StreamReader= File.OpenText("PIONEERS.TXT") lstNames.Items.Clear() Do While sr.Peek <> -1 lstNames.Items.Add(sr.ReadLine) Loop sr.Close()

15 COMP3100e Append (Your Teacher) 'Add a person's name and year of 'birth to file Dim message As String Dim sw as StreamWriter If (txtName.Text <> "") And _ (txtYOB.Text <> "") Then sw = File.AppendText("YOB.TXT") sw.WriteLine(txtName.Text) sw.WriteLine(txtYOB.Text) sw.Close() txtName.Clear() txtYOB.Clear() txtName.Focus() Else message = "You must enter a name and” * _ “ year of birth." MsgBox(message,, "Information “ & _ “Incomplete") End If

16 COMP3100e Change items Dim sr As IO.StreamReader = _ IO.File.OpenText(“COWBOY.TXT") Dim sw As IO.StreamWriter = _ IO.File.CreateText(“COWBOY3.TXT") Dim item as string Dim price as single Do While sr.Peek <> -1 item = sr.readline price= sr.readline If item = "Saddle" Then price = price * 0.8 End If sw.writeline (item) sw.writeline (price) Loop sr.close sw.close MsgBox "New price updated in “ & _ “COWBOY3.TXT",, ""

17 COMP3100e Delete from files Dim sr As IO.StreamReader = _ IO.File.OpenText(“COWBOY.TXT") Dim sw As IO.StreamWriter = _ IO.File.CreateText(“COWBOY3.TXT") Dim item as string Dim price as single Do While sr.Peek <> -1 item = sr.readline price= sr.readline If item <> "Saddle" Then sw.writeln (item) sw.writeln (price) End If Loop sr.close sw.close MsgBox “Saddle removed from “ & _ “COWBOY3.TXT",, ""

18 COMP3100e Structures Structure Individual Dim name As String Dim yearBorn As Integer End Structure Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click 'Sort data from YOB.TXT file by year of birth Dim numPeople As Integer Dim person(numPeople) As Individual numPeople = NumberOfRecords("YOB.TXT") ReadData(person, numPeople) SortData(person, numPeople) ShowData(person, numPeople) WriteData(person, numPeople) End Sub

19 COMP3100e Structures Continued Function NumberOfRecords(ByVal filespec As String) As Integer Dim name As String Dim yearBorn As Integer Dim n As Integer 'Used to count records Dim sr as IO.StreamReader n = 0 File.OpenText(filespec) Do While (sr.Peek <> -1) name = sr.ReadLine yearBorn = CInt(sr.ReadLine) n += 1 'Increase n by 1 Loop sr.Close() Return n End Function

20 COMP3100e Reading into an Array Sub ReadData(ByRef person() As Individual, ByVal numPeople As Integer) 'Read data from file into arrays Dim index As Integer Dim sr As IO.StreamReader = IO.File.OpenText("YOB.TXT") For index = 1 To numPeople person(index).name = sr.ReadLine person(index).yearBorn = CInt(sr.ReadLine) Next sr.Close() End Sub

21 COMP3100e Sorting Sub SortData(ByRef person() As Individual, ByVal numPeople As Integer) 'Bubble sort arrays by year of birth Dim passNum, index As Integer For passNum = 1 To numPeople - 1 For index = 1 To numPeople - passNum If person(index).yearBorn > _ person(index + 1).yearBorn Then SwapData(person, index) End If Next index Next passNum End Sub

22 COMP3100e Showing data in Listbox Sub ShowData(ByVal person() As Individual, ByVal numPeople As Integer) 'Display the sorted list Dim index As Integer lstShowData.Items.Clear() For index = 1 To numPeople lstShowData.Items.Add(person(index).name & " " & person(index).yearBorn) Next End Sub Sub SwapData(ByRef person() As Individual, ByVal index As Integer) Dim temp As Individual temp = person(index) person(index) = person(index + 1) person(index + 1) = temp End Sub

23 COMP3100e Saving Data into a file Sub WriteData(ByVal person() As Individual, ByVal numPeople As Integer) 'Write data back into file Dim index As Integer Dim sr As IO.StreamWriter = IO.File.CreateText("YOB2.TXT" ) For index = 1 To numPeople sr.WriteLine(person(index).name) sr.WriteLine(person(index).yearBorn) Next sr.Close() End Sub

24 COMP3100e The My Object  5 objects available –Application –Computer –Form –User –WebServices

25 COMP3100e Example of My Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MsgBox(My.User.Name) End Sub End Class