+ All Categories
Home > Documents > Variable Scope & Statements

Variable Scope & Statements

Date post: 04-Jan-2016
Category:
Upload: hector-andrews
View: 33 times
Download: 2 times
Share this document with a friend
Description:
Variable Scope & Statements. Try-It 8.8.1 Arrays (yesterday’s assignment). By what two things is each array element identified? The array name and its index or indexes. How many elements are there in an array dimensioned as: Dim sngTemp(100) As Integer. - PowerPoint PPT Presentation
Popular Tags:
17
Variable Scope & Statements
Transcript
Page 1: Variable Scope &  Statements

Variable Scope&

Statements

Page 2: Variable Scope &  Statements

Try-It 8.8.1 Arrays (yesterday’s assignment)

1. By what two things is each array element identified?

The array name and its index or indexes.

Page 3: Variable Scope &  Statements

How many elements are there in an array dimensioned as: Dim sngTemp(100) As Integer

There are actually 101 elements, including sngTemp(0).

Page 4: Variable Scope &  Statements

Write statements to declare the following arrays:

A list of 50 names

Dim strNames(49) As String

Note that an upper limit of 50 actually provides for 51 names, including strNames(0)

Page 5: Variable Scope &  Statements

A list of prices with a maximum index of 100

Dim dcmPrice(100) As String

Note that although the maximum index is 100, this array can actually store 101 items, since there is a 0 index as well.

Page 6: Variable Scope &  Statements

A list of the number of quizzes taken by each student in

a class of 20. Dim intQuizzes(19) As IntegerNote that this array keeps track of just the number of

quizzes taken by each student. It doesn’t keep track of their names!

This array could be dimensioned asDim strQuizzes(19, 1) As StringThen both names and the number of quizzes could be

stored as follows:

strQuizzes(0,0) = “Mary”strQuizzes(0,1) = “9”strQuizzes(1,0) = “Fred”strQuizzes(1,1) = “7”strQuizzes(2,0) = “Sylvia”strQuizzes(2,1) = “6” etc.

Page 7: Variable Scope &  Statements

8.9 - Variable Scope

When discussing Scope?

You are discussing where the variable can be used, and for how long it exists.

Page 8: Variable Scope &  Statements

Declaring variables inside a command button?

Example:

Private Sub cmdGo_Click()

    Dim strMessage As String

    Dim sngSum As Single

End Sub

****This means the variable is only usable by that

command button.****

Page 9: Variable Scope &  Statements

Procedures –vs- Subs?Visual Basic calls command button Click events a

Procedures (or “a section of code”).

Subs are procedures that runs in response to an event.

Note:

The word "sub" actually comes from the word "subroutine", which was used in structured programming languages to refer to a block of code that ran as a single unit.

Page 10: Variable Scope &  Statements

Functions?• Also runs in response to an event, but always

returns a value to the program.Examples:FV(rate, nper, pmt[, pv[, type]])

Returns a Double specifying the future value of an annuity based on periodic, fixed payments and a fixed interest rate.

IsNumeric(expression)

Returns a Boolean value indicating whether an expression can be evaluated as a number.

Round(expression [,numdecimalplaces])

Returns a number rounded to a specified number of decimal places.

Page 11: Variable Scope &  Statements

Local Variables?

What are they?

They are declared inside a procedure.

In other words – they are created when the procedure is executed, maintain their value while the procedure is executing, and then are destroyed when the procedure completes.

Page 12: Variable Scope &  Statements

Static?But, how can you maintain your variable and not destroy it?

Use a Static variable!

Static keyword allows you to declare a local variable that retains its value for as long as the module is loaded.

Example:Private Function strMyFunction() As String

Static intVariable1 As Integer

End Function

Page 13: Variable Scope &  Statements

Module-level variables?

1. Can be declared outside of any Sub or Function in a module or a form.

2. Can be used by any Sub or Function in that module or form.

3. Is declared as Public or Private.– Private can only be used by procedures within that

module.– Public can also be used by procedures in other

modules.Example:

Public MyVariable As IntegerPrivate MyOtherVariable As String

Page 14: Variable Scope &  Statements

Load and Unload?

Unload:– If you property unload your form in your

program than you have successfully destroyed your variables.

Load:– The forms variables will be initialized and

forms LOAD event will be executed.

Page 15: Variable Scope &  Statements

8.10 Statements

Statements:

Are the words you use to tell Visual Basic to do something.

May include:– Required Parameters– Optional Parameters

Page 16: Variable Scope &  Statements

MsgBox?MsgBox statement:

• Prompt: require parameter – the information you want displayed in a message box.

Example: MsgBox ("Hello, World!")

• Buttons: tells whick buttons to display on the message box, and, according to the tip, the message box will default to vbOKOnly if you do not provide some other values.

•Title: This is where you can type the title of your message box. You must enclose your title with quotation marks.

Example: MsgBox ("Hello, world!", vbYesNo, "Hello")

•To display in paragraphs try the following: Example: MsgBox ("An error has occurred." & vbCrLf & vbCrLf & _ "Please contact the system administrator.")

Page 17: Variable Scope &  Statements

vbCrLf?

• A special value know as a Constant.

• Adds a carriage return and a line feed to the message boxes message string.

• Just like pressing the enter key.


Recommended