SlideShare a Scribd company logo
PT1420: File Access and Visual Basic
Page 1
This lab examines how to work with a file by writing Visual
Basic code. Read the following
programming problem prior to completing the lab.
Write and design a simple program that will take 3 pieces of
data and write it to a file. Create
variables and set them equal to the appropriate values:
Declare string firstName = “xxx”
Declare string lastName = “xxx”
Declare integer age = your age
Write this information to a file called myRecords.txt. Next, read
this information from
the file and print it to the screen.
Step 1: Create a new Visual Basic workspace and add the
following code:
Module Module1
Sub Main()
Dim firstName As String = "XXXX"
Dim lastName As String = "XXX"
Dim age As Integer = ####
Dim writer As System.IO.StreamWriter =
System.IO.File.CreateText("myRecords.txt")
writer.WriteLine(firstName & " " & lastName & " " & age)
writer.Close()
Dim reader As System.IO.StreamReader =
System.IO.File.OpenText("myRecords.txt")
Dim myInfo As String = reader.ReadLine()
Console.WriteLine(myInfo)
PT1420: File Access and Visual Basic
Page 2
reader.Close()
'this causes a pause so you can see your program
Console.Write("Press enter to continue...")
Console.ReadLine()
End Sub
End Module
Your module looks like this:
PT1420: File Access and Visual Basic
Page 3
Step 2: Change the values of the variables to your records. Your
module looks like this:
Step 3: Run your program so that it works properly. Your output
might look as follows:
PT1420: File Access and Visual Basic
Page 4
Step 4: Locate the .txt file in the bindebug folder, and view
your .txt file. Your output might
look as follows:
PT1420: File Access and Visual Basic
Page 5
Step 5: Submit the Visual Basic code as a compressed (zipped)
folder using the following steps:
a. Open Windows Explorer --> Start --> All Programs -->
Accessories --> Windows Explorer.
Your Windows Explorer might look as follows:
PT1420: File Access and Visual Basic
Page 6
b. In Windows Explorer, navigate to the folder that contains
your project files. Your
Windows Explorer might look as follows:
(If you don't recall you can check in Visual Studio by opening
your project, right click
module1.vb file and view the properties. Look at the full path
ex.
C:UsersinstructorDocumentsVisual Studio
2010ProjectsmyFirstProgrammyFirstProgramModule1.vb; in
this case navigate to
C:UsersinstructorDocumentsVisual Studio 2010Projects).
Your module properties
might look as follows:
PT1420: File Access and Visual Basic
Page 7
c. Right click on your project folder and choose send to -->
compressed folder. This creates
a zip file of all your code. Your Windows Explorer might look
as follows:
d. Attach the compressed folder you created to your submission.
Your Windows Explorer
might look as follows:
PT1420: Functions in Pseudocode and Visual Basic
Writing Your Own Function that Returns an Integer
Step 1:A function contains three parts: a header, a body, and a
return statement. The first is a function header that specifies the
data type of the value that is to be returned, the name of the
function, and any parameter variables used by the function to
accept arguments. The body is composed of one or more
statements that are executed when the function is called. In the
following space, complete the following: (Reference: Chapter 6,
Writing Your Own Functions, page 225 of your textbook,
Starting Out with Programming Logic & Design).
1. Write a function with the header named addTen.
1. The function will accept an Integer variable named number.
1. The function body will ask the user to enter a number and the
add 10 to the number. The answer will be stored in the variable
number.
1. The return statement will return the value of number.
Function a.__________ a.____________ (b.______________)
Display “Enter a number:”
Input c._________________
Set c._____________ = number + 10
Return d.___________________
Step 2:In the following space, write a function call to your
function from Step 1.
Set number = ____________________ (__________________)
Step 3: Launch and create a new workspace in Visual Basic.
Place the following code inside and run the program. Notice
there is already a call to addTen(number) and that the function
call operates the same ways a module call does, except that a
function call accepts a returned value. Therefore, the function
call looks as number = addTen(number).
Module Module1
Sub Main()
Dim number As Integer = 0
number = addTen(number)
Console.WriteLine("The number with 10 added to it is : " &
number)
Console.WriteLine("Press any key to continue...")
Console.ReadLine()
End Sub
End Module
Your module looks like this:
Step 4: After End Sub in Main(), add a function by typing the
following:
Function addTen(ByVal number As Integer)
When you hit enter, the End Function will automatically be
added. All code in the function will be between Function and
End Function.
Step 5: Write a line of code that will allow the user to enter a
number and then add the formula number = number + 10 based
on the pseudocode in step 1 of this lab.
Step 6: Before End Function, add a return statement that looks
like:
Return number
Now, your module looks like this:
Step 7: Run your program so that if works properly. Your output
might look as follow:
Using Mathematical Library Function: sqrt
Step 8:The sqrt function accepts an argument and returns the
square root of the argument. In the following space, complete
the following: (Reference: The sqrt Function, page 240 of your
textbook, Starting Out with Programming Logic & Design).
1. Declare a variable named myNumber and a variable named
squareRoot of the data type Real.
1. Ask the user to enter a number of which they want to find the
square root. Store the input in myNumber.
1. Call the sqrt function to determine the square root of
myNumber.
1. Display the square root to the screen.
Declare Real a.___________________
Declare Real a.______________________
Display “Enter a number:”
Input b._________________________
Set c.______________ = _______________________
Display “The square root is”, d.____________________
Step 9: Using the same Visual Basic workspace, under the
output from your previous lab, declare and initialize myNumber
and squareRoot as doubles set equal to 0.
Step 10: Write a line of code that will allow the user to enter in
a value for myNumber.
Step 11: Next, add the following line of code which calls the
System.Math.Sqrt() function:
squareRoot = System.Math.Sqrt(myNumber)
Also note the many other Math functions available for use.
Step 12: Add a line of code that will display the value of
squareRoot.
Step 13: Run your program so that if works properly. Your
output might look as follow:
Step 14: Submit the Visual Basic code as a compressed (zipped)
folder using the following steps:
1. Open Windows Explorer --> Start --> All Programs -->
Accessories --> Windows Explorer. Your Windows Explorer
might look as follows:
1. In Windows Explorer, navigate to the folder that contains
your project files. Your Windows Explorer might look as
follows:
(If you don't recall you can check in Visual Studio by opening
your project, right click module1.vb file and view the
properties. Look at the full path ex.
C:UsersinstructorDocumentsVisual Studio
2010ProjectsmyFirstProgrammyFirstProgramModule1.vb; in
this case navigate to C:UsersinstructorDocumentsVisual
Studio 2010Projects). Your module properties might look as
follows:
1. Right click on your project folder and choose send to -->
compressed folder. This creates a zip file of all your code. Your
Windows Explorer might look as follows:
1. Attach the compressed folder you created to your submission.
Your Windows Explorer might look as follows:
Page 1

More Related Content

DOCX
PT1420 Repetition Structures in Visual Basic .docx
DOCX
PT1420 Decision Structures in Pseudocode and Visual Basic .docx
PDF
.NET Portfolio
PDF
C# 12 Pocket Reference: Instant Help for C# 12 Programmers Joseph Albahari
PDF
Bt0082 visual basic
PDF
Creating simple component
PPTX
Vb6.0 intro
PPT
Chapter2.ppt
PT1420 Repetition Structures in Visual Basic .docx
PT1420 Decision Structures in Pseudocode and Visual Basic .docx
.NET Portfolio
C# 12 Pocket Reference: Instant Help for C# 12 Programmers Joseph Albahari
Bt0082 visual basic
Creating simple component
Vb6.0 intro
Chapter2.ppt

Similar to PT1420 File Access and Visual Basic .docx (20)

PDF
Tutorial_Python1.pdf
PPTX
ASP DOT NET
DOC
Cis 247 all i labs
DOCX
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
PPT
2621008 - C++ 1
DOCX
Student Lab Activity A. Lab # CIS CIS170A-A1B. Lab 6.docx
PDF
Software engineering modeling lab lectures
PDF
Diving into VS 2015 Day2
PDF
PVS-Studio vs Chromium. 3-rd Check
DOCX
PT1420 Modules in Flowchart and Visual Basic .docx
DOC
CIS 170 Focus Dreams/newtonhelp.com
DOC
CIS 170 Imagine Your Future/newtonhelp.com   
DOC
CIS 170 Life of the Mind/newtonhelp.com   
PPTX
UNIT I.pptx
DOCX
Cis 170 Extraordinary Success/newtonhelp.com
DOCX
systems labOnce the Application has started up and you are at the .docx
PDF
Prep 2-booklet-2nd-term-2016-2017
PDF
CIS 170 Effective Communication - tutorialrank.com
PPT
Devry cis-170-c-i lab-1-of-7-getting-started
PPT
Devry cis-170-c-i lab-1-of-7-getting-started
Tutorial_Python1.pdf
ASP DOT NET
Cis 247 all i labs
Lab #9 and 10 Web Server ProgrammingCreate a New Folder I s.docx
2621008 - C++ 1
Student Lab Activity A. Lab # CIS CIS170A-A1B. Lab 6.docx
Software engineering modeling lab lectures
Diving into VS 2015 Day2
PVS-Studio vs Chromium. 3-rd Check
PT1420 Modules in Flowchart and Visual Basic .docx
CIS 170 Focus Dreams/newtonhelp.com
CIS 170 Imagine Your Future/newtonhelp.com   
CIS 170 Life of the Mind/newtonhelp.com   
UNIT I.pptx
Cis 170 Extraordinary Success/newtonhelp.com
systems labOnce the Application has started up and you are at the .docx
Prep 2-booklet-2nd-term-2016-2017
CIS 170 Effective Communication - tutorialrank.com
Devry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-started
Ad

More from amrit47 (20)

DOCX
APA, The assignment require a contemporary approach addressing Race,.docx
DOCX
APA style and all questions answered ( no min page requirements) .docx
DOCX
Apa format1-2 paragraphsreferences It is often said th.docx
DOCX
APA format2-3 pages, double-spaced1. Choose a speech to review. It.docx
DOCX
APA format  httpsapastyle.apa.orghttpsowl.purd.docx
DOCX
APA format2-3 pages, double-spaced1. Choose a speech to review. .docx
DOCX
APA Formatting AssignmentUse the information below to create.docx
DOCX
APA style300 words10 maximum plagiarism  Mrs. Smith was.docx
DOCX
APA format1. What are the three most important takeawayslessons.docx
DOCX
APA General Format Summary APA (American Psychological.docx
DOCX
Appearance When I watched the video of myself, I felt that my b.docx
DOCX
apa format1-2 paragraphsreferencesFor this week’s .docx
DOCX
APA Format, with 2 references for each question and an assignment..docx
DOCX
APA-formatted 8-10 page research paper which examines the potential .docx
DOCX
APA    STYLE 1.Define the terms multiple disabilities and .docx
DOCX
APA STYLE  follow this textbook answer should be summarize for t.docx
DOCX
APA7Page length 3-4, including Title Page and Reference Pag.docx
DOCX
APA format, 2 pagesThree general sections 1. an article s.docx
DOCX
APA Style with minimum of 450 words, with annotations, quotation.docx
DOCX
APA FORMAT1.  What are the three most important takeawayslesson.docx
APA, The assignment require a contemporary approach addressing Race,.docx
APA style and all questions answered ( no min page requirements) .docx
Apa format1-2 paragraphsreferences It is often said th.docx
APA format2-3 pages, double-spaced1. Choose a speech to review. It.docx
APA format  httpsapastyle.apa.orghttpsowl.purd.docx
APA format2-3 pages, double-spaced1. Choose a speech to review. .docx
APA Formatting AssignmentUse the information below to create.docx
APA style300 words10 maximum plagiarism  Mrs. Smith was.docx
APA format1. What are the three most important takeawayslessons.docx
APA General Format Summary APA (American Psychological.docx
Appearance When I watched the video of myself, I felt that my b.docx
apa format1-2 paragraphsreferencesFor this week’s .docx
APA Format, with 2 references for each question and an assignment..docx
APA-formatted 8-10 page research paper which examines the potential .docx
APA    STYLE 1.Define the terms multiple disabilities and .docx
APA STYLE  follow this textbook answer should be summarize for t.docx
APA7Page length 3-4, including Title Page and Reference Pag.docx
APA format, 2 pagesThree general sections 1. an article s.docx
APA Style with minimum of 450 words, with annotations, quotation.docx
APA FORMAT1.  What are the three most important takeawayslesson.docx
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Types and Its function , kingdom of life
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Insiders guide to clinical Medicine.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Types and Its function , kingdom of life
GDM (1) (1).pptx small presentation for students
Insiders guide to clinical Medicine.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

PT1420 File Access and Visual Basic .docx

  • 1. PT1420: File Access and Visual Basic Page 1 This lab examines how to work with a file by writing Visual Basic code. Read the following programming problem prior to completing the lab. Write and design a simple program that will take 3 pieces of data and write it to a file. Create variables and set them equal to the appropriate values: Declare string firstName = “xxx” Declare string lastName = “xxx” Declare integer age = your age Write this information to a file called myRecords.txt. Next, read this information from the file and print it to the screen. Step 1: Create a new Visual Basic workspace and add the following code: Module Module1 Sub Main()
  • 2. Dim firstName As String = "XXXX" Dim lastName As String = "XXX" Dim age As Integer = #### Dim writer As System.IO.StreamWriter = System.IO.File.CreateText("myRecords.txt") writer.WriteLine(firstName & " " & lastName & " " & age) writer.Close() Dim reader As System.IO.StreamReader = System.IO.File.OpenText("myRecords.txt") Dim myInfo As String = reader.ReadLine() Console.WriteLine(myInfo) PT1420: File Access and Visual Basic Page 2 reader.Close() 'this causes a pause so you can see your program Console.Write("Press enter to continue...") Console.ReadLine()
  • 3. End Sub End Module Your module looks like this: PT1420: File Access and Visual Basic Page 3 Step 2: Change the values of the variables to your records. Your module looks like this: Step 3: Run your program so that it works properly. Your output might look as follows: PT1420: File Access and Visual Basic Page 4
  • 4. Step 4: Locate the .txt file in the bindebug folder, and view your .txt file. Your output might look as follows: PT1420: File Access and Visual Basic Page 5 Step 5: Submit the Visual Basic code as a compressed (zipped) folder using the following steps: a. Open Windows Explorer --> Start --> All Programs --> Accessories --> Windows Explorer. Your Windows Explorer might look as follows: PT1420: File Access and Visual Basic Page 6
  • 5. b. In Windows Explorer, navigate to the folder that contains your project files. Your Windows Explorer might look as follows: (If you don't recall you can check in Visual Studio by opening your project, right click module1.vb file and view the properties. Look at the full path ex. C:UsersinstructorDocumentsVisual Studio 2010ProjectsmyFirstProgrammyFirstProgramModule1.vb; in this case navigate to C:UsersinstructorDocumentsVisual Studio 2010Projects). Your module properties might look as follows: PT1420: File Access and Visual Basic Page 7 c. Right click on your project folder and choose send to -->
  • 6. compressed folder. This creates a zip file of all your code. Your Windows Explorer might look as follows: d. Attach the compressed folder you created to your submission. Your Windows Explorer might look as follows: PT1420: Functions in Pseudocode and Visual Basic Writing Your Own Function that Returns an Integer Step 1:A function contains three parts: a header, a body, and a return statement. The first is a function header that specifies the data type of the value that is to be returned, the name of the function, and any parameter variables used by the function to accept arguments. The body is composed of one or more statements that are executed when the function is called. In the following space, complete the following: (Reference: Chapter 6, Writing Your Own Functions, page 225 of your textbook, Starting Out with Programming Logic & Design). 1. Write a function with the header named addTen. 1. The function will accept an Integer variable named number. 1. The function body will ask the user to enter a number and the add 10 to the number. The answer will be stored in the variable number. 1. The return statement will return the value of number. Function a.__________ a.____________ (b.______________) Display “Enter a number:” Input c._________________
  • 7. Set c._____________ = number + 10 Return d.___________________ Step 2:In the following space, write a function call to your function from Step 1. Set number = ____________________ (__________________) Step 3: Launch and create a new workspace in Visual Basic. Place the following code inside and run the program. Notice there is already a call to addTen(number) and that the function call operates the same ways a module call does, except that a function call accepts a returned value. Therefore, the function call looks as number = addTen(number). Module Module1 Sub Main() Dim number As Integer = 0 number = addTen(number) Console.WriteLine("The number with 10 added to it is : " & number) Console.WriteLine("Press any key to continue...") Console.ReadLine() End Sub End Module Your module looks like this: Step 4: After End Sub in Main(), add a function by typing the following: Function addTen(ByVal number As Integer) When you hit enter, the End Function will automatically be added. All code in the function will be between Function and End Function. Step 5: Write a line of code that will allow the user to enter a number and then add the formula number = number + 10 based on the pseudocode in step 1 of this lab. Step 6: Before End Function, add a return statement that looks like: Return number
  • 8. Now, your module looks like this: Step 7: Run your program so that if works properly. Your output might look as follow: Using Mathematical Library Function: sqrt Step 8:The sqrt function accepts an argument and returns the square root of the argument. In the following space, complete the following: (Reference: The sqrt Function, page 240 of your textbook, Starting Out with Programming Logic & Design). 1. Declare a variable named myNumber and a variable named squareRoot of the data type Real. 1. Ask the user to enter a number of which they want to find the square root. Store the input in myNumber. 1. Call the sqrt function to determine the square root of myNumber. 1. Display the square root to the screen. Declare Real a.___________________ Declare Real a.______________________ Display “Enter a number:” Input b._________________________ Set c.______________ = _______________________ Display “The square root is”, d.____________________ Step 9: Using the same Visual Basic workspace, under the output from your previous lab, declare and initialize myNumber and squareRoot as doubles set equal to 0. Step 10: Write a line of code that will allow the user to enter in a value for myNumber. Step 11: Next, add the following line of code which calls the System.Math.Sqrt() function: squareRoot = System.Math.Sqrt(myNumber) Also note the many other Math functions available for use. Step 12: Add a line of code that will display the value of
  • 9. squareRoot. Step 13: Run your program so that if works properly. Your output might look as follow: Step 14: Submit the Visual Basic code as a compressed (zipped) folder using the following steps: 1. Open Windows Explorer --> Start --> All Programs --> Accessories --> Windows Explorer. Your Windows Explorer might look as follows: 1. In Windows Explorer, navigate to the folder that contains your project files. Your Windows Explorer might look as follows: (If you don't recall you can check in Visual Studio by opening your project, right click module1.vb file and view the properties. Look at the full path ex. C:UsersinstructorDocumentsVisual Studio 2010ProjectsmyFirstProgrammyFirstProgramModule1.vb; in this case navigate to C:UsersinstructorDocumentsVisual Studio 2010Projects). Your module properties might look as follows: 1. Right click on your project folder and choose send to --> compressed folder. This creates a zip file of all your code. Your Windows Explorer might look as follows:
  • 10. 1. Attach the compressed folder you created to your submission. Your Windows Explorer might look as follows: Page 1