Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.

Similar presentations


Presentation on theme: "Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read."— Presentation transcript:

1 Chapter 4

2  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.  Data Type of the variable – indicates what kind of value it will store.  All variables SHOULD be declared with a Dim statement that includes an identifier and data type. ◦ Example: Dim dblRadius As Double  Variable Declarations – reserves a space in memory for a value.

3  Dim - is a keyword that stands for dimension. A declared variable has “dimension” because it has been assigned space in memory.  Identifier – is the name of the variable.  Data type – indicates what kind of value it will store.

4  Assignment operator – equal symbol (=).  Variable is given a value through assignment.  Variables assignment must be written so the variable is on the left side of the equal sign and the value on the right. ◦ Example: dblRadius = 12.3  If there is an expression on the right side of the equal symbol, the expression is evaluated first and the answer is stored in the memory location

5  Expression must be used on the right side of the assignment. ◦ Example: dblCircleArea = 3.14 * dblRadius ^2

6  Constant is a named memory location which stores a value that cannot be changed at run-time from its initial assignment.  Const statement is used to declare a variable. ◦ Example: Const dblPI As Double = 3.14  Const should be declare before variable declarations.

7  Rules for creating variable names ◦ Must begin with a letter ◦ Must contain only letters, digits, and the underscore (_) character. Periods, spaces, and other special characters are not allowed ◦ Cannot exceed 255 characters.

8  Keywords- has special meaning to the compiler. Cannot be used as user declared variables. ◦ Example: Double, Const  Case Sensitivity in Identifier ◦ Visual Basic is not Case Sensitive.

9  Variables and constants should be named so that they are quickly and clearly understandable to the reader.

10  Built-In Data Types ◦ Single – numbers possibly containing a decimal ◦ Double – numbers possibly containing a decimal ◦ Integer integers only (no decimals) ◦ Long – integers (no decimals) ◦ Currency – numbers representing dollar amounts ◦ String – set of characters ◦ Boolean – True or False

11  Each data type has their own memory requirements for storing values.  Single uses 4 bytes  Double uses 8 bytes  Integer uses 2 bytes  Long uses 4 bytes  Currency uses 8 bytes  Boolean uses 2 bytes  String uses 1 byte for each character in the string

12  Represents positive and negative real numbers (floating point) decimals  Single – can store numbers up to 3.4e 38  Double can store numbers up to 1.8e 308

13  Represents positive and negative integers.  Integer can go up to 32,767 (a number)  Long is used for numbers greater than 32,767.  Trying to store a decimal in an integer location will round up the decimal if it is greater than 5. ◦ 4.7 = 5.0

14  Represents real numbers that are money values.  Currency can store values with up to four digits to the right of the decimal place and 15 digits to the left of the decimal place.

15  Represents a set of characters called string. Can include just about everything, numbers, letters, symbols, spaces etc…, just along as it is in double quotes. ◦ Example: Dim strLastName as String strLastName = “Grassman”

16  Represents True or False  Yes/No or on/off or open/closed

17  Identifiers should be descriptive and begin with an appropriate prefix.  sgl – Single  dbl – Double  int – Integer  lng – Long  cur –Currency  str – String  bln – Boolean

18  A single Dim statement can be used to declare multiple variables using commas. ◦ Example: Dim strName As String, intAge As Integer, dblHeight As Double ◦ Good to put all integers together, strings, etc.  Visual Basic initializes variables when they are declared by default. Double, Single, Integer, Long, & Currency are initialized to 0. String is set to an empty string (“”) and Boolean is set to False. If you don’t set a value they will be set to the default values

19  Visual Basic does not require variables to be declared before they are used.  This may lead to an error in your output. ◦ Example: Dim dblRadius As Double ◦ dblRadis = 10 ‘dblRadius is misspelled ◦ dblCircleArea = dblPi * dblRadius^2 ◦ Answer will be 0 ◦ dblRadius is 0 by default and never assigned to 10 Even though the program works your end result may be incorrect

20  Prevents error as in the previous slide  2 ways to use Option Explicit Statement ◦ In Existing code, select General from the object list in the Code Editor window and then type the statement typed. ◦ To automatically have the Option Explicit Statement appear all the time, go to Tools, Options and select Require Variable Declaration option from the Editor tab

21  Syntax Error – statements that violates the rules of Visual Basic ◦ Example: Dim intX As Integer = 12 ‘Syntax Error – Can’t assign variables in the declaration. ◦ Syntax Error are displayed in Red in the Code Editor.

22  Errors that occur during the application run.  Dialog box displays the error  Clicking the button highlights the statement causing the runtime error.

23 Can’t be divided by zero

24 Debugging – Is a process of correcting errors. Grace Murray Hopper was the first person to apply the term debug.

25  2 ways to get the Immediate Window. ◦ CTRL + G ◦ View menu – Select Immediate window  The Immediate window displays the output of the Debug.Print statement.  Debug.Print item ‘you want printed out  Item is the variable, a string in double quotes, or an expression  Multiple items can be printed out by using the semicolon (;)  When the code is run the Immediate window displays the output of the Debug.Print

26 Private Sub cmdCalculate_Click() Const dblPI As Double = 3.14 Dim dblRadius As Double Dim dblCircleArea As Double Debug.Print "dblRadius ="; dblRadius dblRadius = 10 Debug.Print "dblRadius ="; dblRadius dblCircleArea = dblPI * dblRadius ^ 2 lblAnswer.Caption = dblCircleArea End Sub

27

28  Object that is created using the TextBox control in the Tool box.

29  Name – identifies the object and is used by the programmer. Prefix for naming is txt  Text – changes what is display in the text box. Can be change at run time through assignment. Also changes when user types in the text box.  Alignment – sets the alignment of text relative to the text box.

30  Does not have a Caption  Must provide a label box next to it to act as it Caption.

31  Code for the text box  Is executed when the user begins to type in a text box  At run time, the Text property contains whatever characters the user types into the text box.  The value of the Text box then can be used for assignments, expressions etc.

32  When a decimal number ends with.5. Visual Basic will round up if the decimal number is an odd number and round down if the decimal number is even.  Example: Dim intX as Integer intX = 5.5 ‘intX is assigned 6 because intX is an Integer and 5 is odd intX = 6.5 ‘intX is assigned 6 because intX is an Integer and 6 is even

33  Visual Basic automatically converts data to match the type of the variable it is being assigned to.  (Implicit-computer changes it) ◦ Example: Dim intSum As Integer intSum = 6.7 ‘intSum will get 7  Data typed in a text box is a String  The String will be converted by Visual Basic to the appropriate data type when assigning it.

34  Trying to assign a value into an inappropriate data type causes an error. ◦ Example: intSum = “abc” ‘Trying to assign a string to an Integer.

35  \ - Integer Division – truncates the decimal portion of the quotient which results in an Integer. ◦ Example: intSum = 20\7 ‘intSum is assigned 2  Mod – Modulus Division – returns the remainder resulting from division. ◦ Example: intSum = 20 Mod 7 ‘intSum is assigned 6

36  ^  *, /  \  Mod  +, -

37  Also called radio buttons  Another way to get input from user.

38  Name – identifies the object and is used by the programmer. The prefix for Option Button names is “opt”  Caption – Changes the text displayed as the option button label. Can be change at run time.  Value – Can be set to True and False. Only one option button can be true at any given time.  Alignment – Moves option button left, right, or center.

39  A click event procedure is usually coded for each option button.  Is executed when user clicks on an option button

40  Is a container object to group option buttons relative to a single choice.

41  Name – identifies the object and is used by programmer. Prefix for frames is “fra”  Caption – changes the text displayed as the frame label.

42

43  Create frame first and then select OptionButton control and drag over to frame.  When dragging the frame, the option buttons will move with the frame.

44  Object names that begin with 3 letters descriptive of the object.  Comments that explain and clarify code  Use parentheses in Expressions if needed to better understand what is intended.  Variables and constants with meaningful identifiers to represent values. Prefix identifies data type  Option Explicit statement  Variables of the appropriate data type 5 * 3 + 2 * 3 Better written as (5 * 3) + (2* 3)

45  Variables declared at the beginning of the procedure that they are used in  Constants to represent unchanging values  Constants declared at the beginning before variable declarations  Statements in the procedures are indented with a tab


Download ppt "Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read."

Similar presentations


Ads by Google