Variables Constants and Calculations1 Chapter 3 we are

Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need to be able to perform calculations we will learn about Variables, constants Data types Hungarian notation Scope of a variable Operations on variables … and more! 110 -D 1

Computer and Memory Disk Central Processing Unit Main Memory Monitor Keyboard mouse Network Understand how the computer uses its memory from a programmer's point of view 110 -D 2

Memory and Data All data used by a program is stored in the memory of the computer (numbers, names…). Think of memory as a (huge) pile of boxes 5 Each box has a number gives the location 4 3 Each box always contains a value: 2 a succession of 0’s and 1’s 1 0 BUT can be interpreted in many ways by a program (a number, a letter. . ) 110 -D 3

Memory and Data Location 4 contains 0. 981 4 109 3 ‘c’ 2 4 1 3. 1415 0 Location 2 contains the letter ‘c’ In a program do not use the location number (=address) explicitly: don’t want to say “take the content of box 4” would drive us crazy! 110 -D 4

Instead: Use a name to refer to the content of a given box a variable Rule of programming: A variable MUST be declared to indicate the type of the variable (is it a character (letter), an integer, a floating point number…? ) VB. NET strictly enforces the rule. By default it has Option Explicit on. Don’t turn it off! It is also a good idea to turn Option Strict on to prevent accidental conversions between types (see later). 110 -D 5

Declaring Variables Many different types: some examples A VB language declaration Dim int. Number. Of. Children As Integer VB key word. Reserve space in the memory For integer int for integer (Hungarian notation) No space or periods in the variable name Can use underscores (_) Use meaningful names (NOT Dim n As Integer) An integer can be 1, -46, 236 (NOT 1. 5, -2. 3) 110 -D 6

Dim dbl. Temperature As Double for a floating point, that is 96. 2, -14. 9, -7. 02 e-11. . . Dim str. Message As String for a string: any combination of characters e. g. "123 go", "#!@ ", "Blu. E" (the content of a string is written between double quotes ("), e. g. str. Message = "Welcome to VB. NET") 110 -D 7

Naming Rules (1) To make your code easier to understand, use the following stylistic guidelines: _ precede the name of every variable, constants, or object with a lowercase prefix specifying the data type. (Hungarian notation: see list p 99) _ Capitalize each first letter of each word of a name, e. g. int. Number. Of. Children _ use letters, numbers: str. Employee 1 SSN _ can’t start with a number or underscore: _bad, 1_not_good _ Names can’t contain spaces or periods 110 -D 8

Naming Rules (2) _can’t be a VB. NET keyword: End, Dim, As. . . _NOT case sensitive: temperature is the same as Temperature _ length: practically, as long as you want (the limit is 16, 383 characters, but. . . ) _ meaningful names: Dim int. Age As Integer NOT Dim a As Integer VB will help you as you type. . . 110 -D 9

Constants Within a program, some values do not change: e. g. value of pi = 3. 14159 To avoid any accidental change, declare such values as constants: declares a constant Const dbl. PI As Double = 3. 14159 Initialize the constant when declaring it (can’t do it anywhere else!) Can have the same types for constants as for variables Use only uppercase letters for the name (except for the prefix) Integer, Double, String. . . VB provides a set of built in constants. (intrinsic constants). You need to specify class name and constant name, e. g. Color. Red 110 -D 10

Some common types We have seen: String, Integer, Double Also: Boolean For logical values (True/False) Dim bln. Light. On As Boolean Decimal For decimal fractions such as dollars and cents Dim dec. My. Balance As Decimal Object When you don’t specify the type of a variable, VB. NET takes it as an object. An object type variable can hold any type of data. optional Dim obj. Anything Dim obj. Something As Object Uses lots of memory, not as efficient. Avoid if you can 110 -D 11

Storing values into variables Declare a variable gives a name to the content of a memory location We get a box. But how to put something in it? Or better said: How to store the value of a variable in the memory? One way: Use an assignment statement a declaration Dim str. Name As String str. Name = "Jane Beckmann" an assignment statement an expression (anything that has a value) Note: to get " in a string, write "" str. Dialog = "She said ""Well, . . . """ to get She said "Well, . . . " 110 -D 12

Processing an assignment statement Consider: dbl. Perimeter = dbl. PI*dbl. Diameter 1 Evaluate the right hand side (=expression) dlb. PI*dbl. Diameter 2 The value is stored in the left hand side, the assignment variable dbl. Perimeter Note: can have int. Year = int. Year + 1 (NOT a mathematical equation!) 110 -D 13
- Slides: 13