advanced vb: review of the basics

17
REVIEW OF THE BASICS Advanced Visual Basic

Upload: robertbenard

Post on 18-Dec-2014

290 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Advanced VB: Review of the basics

REVIEW OF THE BASICS

Advanced Visual Basic

Page 2: Advanced VB: Review of the basics

Overview

You are now in at least your third semester of programming. In this course, we will be exploring more of Visual Basic. Before doing so, we should re-familiarize ourselves with the development environment and some of the basic programming topics.

Page 3: Advanced VB: Review of the basics

Variables

You often have to store values when you perform calculations with Visual Basic. For example, you might want to calculate several values, compare them, and perform different operations on them, depending on the result of the comparison. You have to retain the values if you want to compare them.

Variables store values in RAM. A variable has a name (the word that you use to refer to the value that the variable contains). A variable also has a data type (which determines the kind of data that the variable can store).

Example:

Dim strFirstName As String

Page 4: Advanced VB: Review of the basics

Variables

Research Question:

What is the difference between a variable and a constant?

Page 5: Advanced VB: Review of the basics

Arrays

An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school.

An array allows you to refer to these related values by the same name and to use a number, called an index or subscript, to tell them apart. The individual values are called the elements of the array. They are contiguous from index 0 through the highest index value.

Example:

Dim strStudentNames(10) As String

Page 6: Advanced VB: Review of the basics

Arrays

Research Question:

How many elements are in this array?

Dim strStudentNames(10) As String

Page 7: Advanced VB: Review of the basics

Loops

Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.

Page 8: Advanced VB: Review of the basics

Loops (while)

Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True.

Example:

Dim counter As Integer = 0 While counter < 3

counter += 1 ‘Insert code to use current value of counter.

End While MsgBox("While loop ran " & CStr(counter) & " times")

Page 9: Advanced VB: Review of the basics

Loops (do)

Use a Do...Loop structure when you want to repeat a set of statements an indefinite number of times, until a condition is satisfied.

Example:

Dim counter As Integer = 0 Dim number As Integer = 10Do Until number = 100

number = number * 10 counter += 1

Loop MsgBox("The loop ran " & counter & " times.")

Page 10: Advanced VB: Review of the basics

Loops (for…next)

Use a For...Next structure when you want to repeat a set of statements a set number of times.

Example:

For index As Integer = 1 To 5 Debug.Write(index.ToString & " ")

Next Debug.WriteLine("") ' Output: 1 2 3 4 5

Page 11: Advanced VB: Review of the basics

Loops (for each)

Use a For Each...Next loop when you want to repeat a set of statements for each element of a collection or array.

Example:Sub BlueBackground(ByVal thisForm As System.Windows.Forms.Form) For Each thisControl As System.Windows.Forms.Control In thisForm.Controls

thisControl.BackColor = System.Drawing.Color.LightBlue Next thisControl

End Sub

Page 12: Advanced VB: Review of the basics

Loops

Research Question:

What happens when a loop doesn’t end?

Dim intValue as integer = 1Do Until intValue = 2

MessageBox.Show(“you have a problem!”)

Loop

Page 13: Advanced VB: Review of the basics

Decisions

Visual Basic lets you test conditions and perform different operations depending on the results of that test. You can test for a condition being true or false, for various values of an expression, or for various exceptions generated when you execute a series of statements.

Page 14: Advanced VB: Review of the basics

Decisions (if…then…else)

When an If...Then...Else statement is encountered, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf statement (if there are any) is evaluated in order. When a True elseifcondition is found, the statements immediately following the associated ElseIf are executed. If no elseifcondition evaluates to True, or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then, ElseIf, or Else, execution continues with the statement following End If.

Example:

Dim count As Integer = 0 Dim message As String If count = 0 Then

message = "There are no items." ElseIf count = 1 Then

message = "There is 1 item." Else

message = "There are " & count & " items." End If

Page 15: Advanced VB: Review of the basics

Decisions (if…then…else)

The Case Else statement is used to introduce the elsestatements to run if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. Although not required, it is a good idea to have a Case Else statement in your Select Case construction to handle unforeseen testexpression values

Example:Dim number As Integer = 8 Select Case number Case 1 To 5

Debug.WriteLine("Between 1 and 5, inclusive")Case 6, 7, 8

Debug.WriteLine("Between 6 and 8, inclusive") Case 9 To 10

Debug.WriteLine("Equal to 9 or 10") Case Else

Debug.WriteLine("Not between 1 and 10, inclusive") End Select

Page 16: Advanced VB: Review of the basics

Decisions

Research Question:

What is the most efficient way to determine if someone can vote?

Consider: Age = 17Age = 18Age = 19

Page 17: Advanced VB: Review of the basics

Additional Information

For additional information about these topics, please review your text and the links provided in Blackboard.