170 likes | 253 Views
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.
E N D
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 There are actually 101 elements, including sngTemp(0).
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)
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.
A list of the number of quizzes taken by each student in a class of 20. Dim intQuizzes(19) As Integer Note 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 as Dim strQuizzes(19, 1) As String Then 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.
8.9 - Variable Scope When discussing Scope? You are discussing where the variable can be used, and for how long it exists.
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.****
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.
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.
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.
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
Module-level variables? • Can be declared outside of any Sub or Function in a module or a form. • Can be used by any Sub or Function in that module or form. • 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 Integer Private MyOtherVariable As String
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.
8.10 Statements Statements: Are the words you use to tell Visual Basic to do something. May include: • Required Parameters • Optional Parameters
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.")
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.