ITP 150 Week 4 Variables
ITP Lecturer: A. Borquez2 Review: Controls Propeties Events Methods Procedures Functions BackStyleFillColor BorderStyleFillStyle CaptionName EnabledVisible Move Clear SetFocusAddItem ActivateDragOverLoad ChangeGotFocusLostFocus ClickKeyDownMouseDown DblClickKeyPressMouseMove DragDropKeyUpMouseUp
ITP Lecturer: A. Borquez3 Controls for Displaying and Entering Text To provide this feature Use this control Text that can be edited by the user, for example Text box an order entry field or a password box Text that is displayed only, for example to identify Label a field on a form or display instructions to the user
Controls That Present Choices to Users To provide this feature Use this control A small set of choices from which a user can choose one or Option buttons more options. Check boxes A small set of options from which (use frames if a user can choose just one. additional groups are needed) A scrollable list of choices from which the user can choose. List box A scrollable list of choices along with a text edit field. Combo box The user can either choose from the list or type a choice in the edit field.
Controls That Display Pictures and Graphics To provide this featureUse this control A container for other controls.Picture box Printing or graphics methods. Picture box Displaying a picture. Image control or picture box Displaying a simple graphical element Shape or line control
ITP Lecturer: A. Borquez6 Managing Forms
ITP Lecturer: A. Borquez7 Variables In Visual Basic, you use variables to temporarily store values during the execution of an application Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store) To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable: Dim variablename [As type]
ITP Lecturer: A. Borquez8 Variable rules…... Must begin with a letter. Can't contain an embedded period or embedded type- declaration character. Must not exceed 255 characters. Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.
ITP Lecturer: A. Borquez9 Visual Basic Variables Variables: temporarily store values during the execution of an application. Variables have a: name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store). Variables can be used in many ways, including: As a counter that stores the number of times a procedure or code block executes. As a temporary storage for property values. As a place to hold a value returned from a function. As a place to store directory or file names.
ITP Lecturer: A. Borquez10 Other Variable issues to think about…. Implicit vs. Explicit Declarations Declaring a variable in the Declarations section of a form, standard, or class module, rather than within a procedure, makes the variable available to all the procedures in the module. Declaring a variable using the Public keyword makes it available throughout your application. Declaring a local variable using the Static keyword preserves its value even when a procedure ends.
ITP Lecturer: A. Borquez11 Variable Data Types Data typeStorage size Byte1 byte Boolean2 bytes Integer2 bytes Long (long integer)4 bytes Single (single floating-point)4 bytes Double(double floating-point)8 bytes Currency (scaled integer)8 bytes Date8 bytes Object4 bytes + size of object String (variable-length)10 bytes + string length String (fixed-length)length of string Variant16 bytes Data typeStorage size Byte1 byte Boolean2 bytes Integer2 bytes Long (long integer)4 bytes Single (single floating-point)4 bytes Double(double floating-point)8 bytes Currency (scaled integer)8 bytes Date8 bytes Object4 bytes + size of object String (variable-length)10 bytes + string length String (fixed-length)length of string Variant16 bytes
ITP Lecturer: A. Borquez12 Variable Data Types Naming Conventions Data typePrefix Bytebyt Booleanbln Integerint Longlng Singlesng Doubledbl Currencycur Datedtm Objectobj Stringstr Variantvar Data typePrefix Bytebyt Booleanbln Integerint Longlng Singlesng Doubledbl Currencycur Datedtm Objectobj Stringstr Variantvar
Understanding the Scope of Variables Scope Private Public Procedure-level Variables are private to Not applicable. You the procedure in which cannot declare public they appear. variables within a procedure. Module-level Variables are private toVariables are available the module in which to all modules. they appear.
ITP Lecturer: A. Borquez14 Introduction to Procedures There are two major benefits of programming with procedures: Procedures allow you to break your programs into discrete logical units, each of which you can debug more easily than an entire program without procedures. Procedures used in one program can act as building blocks for other programs, usually with little or no modification.
ITP Lecturer: A. Borquez15 Working with Date and Time Dim dtNewDate As Date dtNewDate = dtDate + 5 MsgBox "The current date and time is " & Now() MsgBox "The date is " & Date() MsgBox "The time is " & Time() Now, Date, and Time functions
ITP Lecturer: A. Borquez16 VB Date/Time Functions FunctionExample Value displayed Year()Year(Now) 1999 Month()Month(Now) 2 Day()Day(Now) 22 Weekday()Weekday(Now) 7 Hour()Hour(Now) 11 Minute()Minute(Now) 38 Second()Second(Now) 9
The DateDiff Function The DateDiff function returns the number of time intervals between two dates. DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) Dim dtTheDate As Date Dim strMsg As String dtTheDate = InputBox("Enter a date") strMsg = "Days from today: " & DateDiff("d", Now, dtTheDate) MsgBox strMsg
VB Format Function the Format function accepts a numeric value and converts it to a string Format syntaxResult Format(Now, “m/d/yy”)1/27/99 Format(Now, “dddd, mmmm dd, yyyy”)Wednesday, January 27, 1999 Format(Now, “d-mmm”)27-Jan Format(Now, “mmmm-yy”)January-99 Format(Now, “hh:mm AM/PM”)07:18 AM Format(Now, “h:mm:ss a/p”)7:18:00 a Format(Now, “d-mmmm h:mm”)3-January 7:18 txtDate.Text = Format(Now, "Long Date") txtCost.Text = Format(1000, "Currency") Using Named Formats
ITP Lecturer: A. Borquez19 Sub Procedures vs. Function Procedures Sub procedures do not return a value. Function procedures return a value. Sub procedures do not return a value. Function procedures return a value.
ITP Lecturer: A. Borquez20 A Closer Look at Sub_Procedures... A Sub procedure is a block of code that is executed in response to an event. By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application. A Sub procedure is a block of code that is executed in response to an event. By breaking the code in a module into Sub procedures, it becomes much easier to find or modify the code in your application. [Private|Public][Static]Sub procedurename (arguments) statements Each time the procedure is called, the statements between Sub and End Sub are executed. Sub procedures can be placed in standard modules, class modules, and form modules. Sub procedures are by default Public in all modules Each time the procedure is called, the statements between Sub and End Sub are executed. Sub procedures can be placed in standard modules, class modules, and form modules. Sub procedures are by default Public in all modules
A Closer Look at Functions…. Visual Basic includes built-in, or intrinsic functions, like Sqr, Val, or Chr. You can use the Function statement to write your own Function procedures. Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. Function procedures have data types, just as variables do. You return a value by assigning it to the procedurename itself. [Private|Public][Static]Function procedurename (arguments) [As type] statements