140 likes | 232 Views
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
E N D
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-D1
Computer and Memory Monitor Central Processing Unit Disk Main Memory mouse Keyboard Network Understand how the computer uses its memory from a programmer's point of view 110-D2
Memory and Data Think of memory as a (huge) pile of boxes Each box has a number gives the location 5 4 Each box always contains a value: 3 a succession of 0’s and 1’s 2 1 BUT can be interpreted in many ways by a program (a number, a letter..) 0 All data used by a program is stored in the memory of the computer (numbers, names…). 110-D3
Memory and Data 4 0.981 3 109 2 ‘c’ 4 1 3.1415 0 Location 4 contains 0.981 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-D4
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-D5
Declaring Variables A VB language declaration Dim intNumberOfChildren 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) Many different types: some examples An integer can be 1, -46, 236 (NOT 1.5, -2.3) 110-D6
Dim dblTemperature As Double for a floating point, that is 96.2, -14.9, -7.02e-11... Dim strMessage As String for a string: any combination of characters e.g. "123go", "#!@ ", "BluE" (the content of a string is written between double quotes ("), e.g. strMessage = "Welcome to VB.NET") 110-D7
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. intNumberOfChildren _ use letters, numbers: strEmployee1SSN _ can’t start with a number or underscore: _bad, 1_not_good _ Names can’t contain spaces or periods 110-D8
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 intAge As Integer NOT Dim a As Integer VB will help you as you type... 110-D9
Constants declares a constant Initialize the constant when declaring it (can’t do it anywhere else!) Use only uppercase letters for the name (except for the prefix) Can have the same types for constants as for variables Integer, Double, String... 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: Const dblPI As Double = 3.14159 VB provides a set of built in constants. (intrinsic constants). You need to specify class name and constant name, e.g. Color.Red 110-D10
Some common types Boolean Decimal For logical values (True/False) For decimal fractions such as dollars and cents Dim blnLightOn As Boolean Dim decMyBalance 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 objAnything Dim objSomething As Object We have seen:String, Integer, Double Also: Uses lots of memory, not as efficient. Avoid if you can 110-D11
Storing values into variables gives a name to the content of a memory location One way: Use an assignment statement a declaration an expression (anything that has a value) an assignment statement Note: to get " in a string, write "" strDialog = "She said ""Well,...""" to get She said "Well, ..." Declare a variable 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? Dim strName As String strName = "Jane Beckmann" 110-D12
Processing an assignment statement 1 Evaluate the right hand side (=expression) dlbPI*dblDiameter 2 The value is stored in the left hand side, the assignment variable dblPerimeter Consider: dblPerimeter = dblPI*dblDiameter Note: can have intYear = intYear + 1 (NOT a mathematical equation!) 110-D13