0% found this document useful (0 votes)
26 views

Revision

The document discusses arrays in programming. It explains that arrays allow the capture and manipulation of multiple values of the same data type. Declaring an array involves specifying the array name, its size or upper limit, and its data type. Arrays store elements in sequential memory locations that can be accessed using an index number. The document provides examples of declaring and initializing integer arrays in Visual Basic. It also discusses some benefits of using arrays, such as performing calculations on array elements using loops.

Uploaded by

cv5mv6w4ht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Revision

The document discusses arrays in programming. It explains that arrays allow the capture and manipulation of multiple values of the same data type. Declaring an array involves specifying the array name, its size or upper limit, and its data type. Arrays store elements in sequential memory locations that can be accessed using an index number. The document provides examples of declaring and initializing integer arrays in Visual Basic. It also discusses some benefits of using arrays, such as performing calculations on array elements using loops.

Uploaded by

cv5mv6w4ht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Variables

What are they used for?

What is the relationship between a variable and a data type?

What is the scope and duration of a variable?

What is a constant?
Arrays in Programming

Problem
If you want to capture 10 numbers and then manipulate them in
some way you would currently have to declare 10 separate
variables.

Dim varNum1, varNum2 as Integer


Dim varNum3, varNum4 as Integer
Dim varNum5, varNum6 as Integer
Dim varNum7, varNum8 as Integer
Dim varNum9 as Integer
Dim varNum10 as Integer
Arrays in Programming
Problem
Any manipulation of these numbers would require assignment statements
listing the whole variables .
varSum = varNum1 +varNum2 + varNum3 + varNum4 + varNum5 + varNum6 + varNum7 + varNum8 + varNum9 +
varNum10

varAvg = (varNum1 +varNum2 + varNum3 + varNum4 + varNum5 + varNum6 + varNum7 + varNum8 + varNum9 +
varNum10)/10
Arrays in Programming
Arrays
An array is a list of sequential memory locations of the same data type. An
array is identified by a unique name and each element within the array is
addressed by its index(subscript) number.

Array Name: arrNumbers: 10 20 30 40 50 60 70 80 90 100

Index: 1 2 3 4 5 6 7 8 9 10

arrNumbers(2) = 20
Arrays in Programming
Basic Array Definition
1. Every element in an array has to be of the same data
type (all integers, all Booleans, all strings, etc).
2. Each element is addressed by its index number,
starting at 0.
3. It’s good practice to make the array a set size before
putting values in it.
Declaring an Array
Dim ArrayName(UpperSubscript) As DataType
• ArrayName is the variable name of the array
• UpperSubscript is the value of the array's
highest subscript
• Must be a positive whole number
• DataType may be any Visual Basic data type
• VB knows an array is declared when name is
followed by parentheses with number of
elements
Arrays in Programming
Declaration ( 1 dimensional )

Array upper limit

Dim arrNumbers(10) as Integer


Data Type

Array Name
This declaration sets up 10 consecutive memory
locations because VB.net includes the a zero
index arrNumbers(0) … arrNumbers(9)
Declaring an Array
In BASIC, arrays are declared (created, instantiated)
using the DIM (short for DIMENSION) statement
Includes the name of the array, and its size, e.g.

dim arrCost(12) as single


Upper limit is specified
Why use an Array
• forces variables to all be of the same type.
• can use loops to do calculations (like adding up)
quickly and easily.
• easier to search an array than many different
variables.
varSum = varNum1 +varNum2 + varNum3 + varNum4 + varNum5 + varNum6 + varNum7 + varNum8 + varNum9 + varNum10

Dim varSum as Integer


Dim arrNum(10) as Integer

varSum = 0
For index = 0 to 9
varSum = varSum + arrNum(index)
Next
Record Structure
Student (StudentNO, Name,Surname,Dob,YearLevel, House)
Attribute Data Type
StudentNO String
Name String
Surname String
Dob Date
YearLevel Integer
House String

A record (class) structure can be defined


to store multiple related attributes
Record Structure
Student (StudentNO, Name,Surname,Dob,YearLevel, House)
Student Attributes
StudentNO
Name
Surname
Dob
YearLevel
House

The record Student contains the following attributes.


You cannot refer to just the term Student because that would
be ambiguous.
Student.StudentNO or Student.Name, or Student.YearLevel
Record Structure – (User Defined Data Structure)
Student (StudentNO,Name,Surname,Dob,YearLevel, House)
Structure Student Class Product
Public StudentNO As String Public StudentNO As String
Public Name As String Public Name As String
Public Surname As String Public Surname As String
Public Dob As Date Public Dob As Date
Public YearLevel As Integer Public YearLevel As Integer
Public House As Integer Public House As Integer
End Structure End Class
Reference: Student.StudentNO = “S100” With Student
Student.Name = “Bill” .StudentNO = “S100”
Student.Surname = “Smith” .Name = “Bill”
Student.Dob = #25/04/2000# .Surname = “Smith”
Student.YearLevel = 11 .Dob = #25/04/2000#
Student.House = “Hilton” .YearLevel = 11
.House = “Hilton”
End With
Declaring an Array of Records
Dim arrStudent(6) As Student
Index StudentNO Name Surname Dob YearLevel House
0 S100 Bill Smith 25/04/2000 11 Hilton
1 S200 Anne Jones 18/05/2001 12 Braeside
2 S300 Kevin Bleedy 04/05/2001 10 Bacchus
3 S400 James Hill 25/04/2000 12 Pentland
4 S500 Daniela Wong 13/03/2002 11 Bacchus
5 S600 Helen Hunt 25/10/2002 10 Hilton

The Student attributes are all together in one array


XML Introduction
• Derived from SGML (Standard Generalized Markup Language)
• Text-based format
• XML stands for Extensible Markup Language.
• Describes document structures using markup tags
• Useful for describing document formats for Web
• It is also useful for describing both structured as well as semi-
structured data.
• Platform-independent
• Can be run over any operating System
• Can be processed using any programming language
• Extensible
• No fixed vocabulary
• Flexible, change the structure by adding new tags
• Supports Global implementation being fully Unicode
• No emphasis on display and rendering the XML document
XML Introduction
• XML uses tags to define and structure data.
• XML tags are not used to render text like HTML tags.

HTML XML

<bold> Hello World <student>


</bold> <name> Bill </name>
<surname>Smith</surname>
</student>

Browser interprets the tag. A browser ignores the tags because they
Turns on bold print and off are not defined
XML Introduction
XML Document Structure
An XML document consists of a prolog and elements.

The prolog indicates the version of xml being applied to the document and the
language set used. <?xml version="1.0" encoding="UTF-8"?>

The data within an XML document is structured into elements. The elements
are structured into a hierarchy. <root>
<child>
<subchild>.....</subchild>
</child>
</root>
XML Element Rules

• Element names are case-sensitive


• Element names must start with a letter or underscore
• Element names cannot start with the letters xml (or XML, or Xml, etc)
• Element names can contain letters, digits, hyphens, underscores, and periods
• Element names cannot contain spaces
• An element must have an opening and closing tag
<student>
<name>Bill</name>
<surname>Smith</surname>
<tutorgrp> 12C</tutorgrp>
</student>
• Elements must be properly nested
• Any name can be used, no words are reserved (except xml).
XML Element Text and Attributes

• A Data value can be stored in an Element by using its text or attribute


component.

<student sid = “S12345”> <student>


<name>Bill</name> <sid>S12345</sid>
<surname>Smith</surname> <name>Bill</name>
<tutorgrp> 12C</tutorgrp> <surname>Smith</surname>
</student> <tutorgrp> 12C</tutorgrp>
</student>
Attribute Text Values

• Data in an attribute must be enclosed in quotation marks. names must start


with a letter or underscore
XML Document Structure

<?xml version="1.0" encoding="UTF-8"?> Declaration

<PRODUCTDATA> Root Element


Parent Element
<PRODUCT PRODID="P001"> Child
<PRODUCTNAME>Windows XP SP2</PRODUCTNAME> Element
Attribute <DESCRIPTION>
This is the latest update pack provided by Microsoft.
</DESCRIPTION>
<DETAILS>Has Firewall Protection</DETAILS>
<PRICE>Free</PRICE>
<SIZE>~200 MB for Windows XP Home Edition</SIZE>
</PRODUCT>
Content
</PRODUCTDATA>
XML Comments

Following rules should be followed for XML comments −


•Comments cannot appear before XML declaration.
•Comments may appear anywhere in a document.
•Comments must not appear within attribute values.
•Comments cannot be nested inside the other comments.

<!-- This is a comment --> <?xml version = "1.0" encoding = "UTF-8" ?>
<!--Students grades are uploaded by months-->
<class_list>
<student>
<name>Anne</name>
<grade>A</grade>
</student>
</class_list>
Procedures
• A procedure contains a block of common code that can be called from with the main
body of a program
• A procedure may use input parameters
• It doesn’t need to be assigned to a variable. Hence there is no return value.
Structure
Private/Public Sub ProcedureName( byVal/byRef parameters as datatype)
End Sub
Example Private Sub proClearForm()
txtGross.Text = ""
txtHours.Text = ""
txtNetPay.Text = ""
txtRate.Text = ""
txtTax.Text = ""
End Sub
Declaring a Procedures
Structure
Private/Public Sub ProcedureName( byVal parameters as datatype)
End Sub
Parameters refers to a list of variables the procedure requires
during it’s execution

Declaration Private Sub proHideShowButtons(byval parState as boolean)


btnPrevious.visible = parState
btnNext.visible = parState
btnSave.visible = parState
btnCancel.visible = parState
End Sub
Call
proHideShowButtons(true)
proHideShowButtons(false)
proHideShowButtons(varState)
Procedures
Structure
Private/Public Sub ProcedureName( byVal/byRef parameters as datatype)
End Sub No input parameters One input parameters

Private Sub proClearForm() Private Sub proDisplay(parIndex)


txtGross.Text = "" With arrStudent(parIndex)
txtHours.Text = "" txtStudNO.Text = .StudentNo
txtNetPay.Text = "" txtName.Text = .Student
txtRate.Text = "" txtTutGrp.Text = .TutorGrp
txtTax.Text = "" End With
End Sub End Sub
Procedure Examples
proHideShowButtons(true)

Execution Private Sub proHideShowButtons(byval parState as boolean)


btnPrevious.visible = true parState = true
btnNext.visible = true
btnSave.visible = true
btnCancel.visible = true
End Sub

proHideShowButtons(false)
Private Sub proHideShowButtons(byval parState as boolean)
btnPrevious.visible = false parState = false
btnNext.visible = false
btnSave.visible = False
btnCancel.visible = false
End Sub
Procedure Examples
varState = true
proHideShowButtons(varState)

Execution Private Sub proHideShowButtons(byval parState as boolean)


btnPrevious.visible = parState parState = varState = true
btnNext.visible = parStare
btnSave.visible = parState
btnCancel.visible = parState
End Sub
Procedures
Private Sub proClearForm()
txtGross.Text = ""
txtHours.Text = ""
txtNetPay.Text = ""
txtRate.Text = ""
txtTax.Text = ""
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnReset.Click

' Clears All the text boxes


proClearForm()
End Sub
Data validation

• Existence
• that all required fields have a value.
• Type
• that an incoming value is the right data type
(ie. numeric, date, etc.).
• Range
• that an input value is within the set of legal values.
• Length
• that an input string has the correct number of characters.
• Format
• that an input string matches the formatting pattern expected.
• Cross-field
• that values in two or more fields are consistent.
Errors that can occur in
programming
•Syntax
•Logical
•Run Time

You might also like