Download presentation
Presentation is loading. Please wait.
Published byShauna Norman Modified over 9 years ago
1
110-D1 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!
2
110-D2 Computer and Memory Central Processing Unit Main Memory Monitor Disk Keyboard mouse Network Understand how the computer uses its memory from a programmer's point of view
3
110-D3 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 0 1 2 3 4 5 Each box has a number gives the location BUT can be interpreted in many ways by a program (a number, a letter..) Each box always contains a value: a succession of 0’s and 1’s
4
110-D4 Memory and Data 0 1 2 3 4 0.981 109 ‘c’ 4 3.1415 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!
5
110-D5 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).
6
110-D6 Declaring Variables Many different types: some examples Dim intNumberOfChildren As Integer A VB language declaration VB key word. Reserve space in the memory int for integer (Hungarian notation) No space or periods in the variable name Can use underscores (_) Use meaningful names (NOT Dim n As Integer ) For integer An integer can be 1, -46, 236 (NOT 1.5, -2.3)
7
110-D7 Dim dblTemperature As Double for a floating point, that is 96.2, -14.9, -7.02e-11... 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") Dim strMessage As String
8
110-D8 _ precede the name of every variable, constants, or object with a lowercase prefix specifying the data type. (Hungarian notation: see list p 99) Naming Rules (1) To make your code easier to understand, use the following stylistic guidelines: _ 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
9
110-D9 Naming Rules (2) _ meaningful names: Dim intAge As Integer NOT Dim a As Integer VB will help you as you type... _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...)
10
110-D10 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: Const dblPI As Double = 3.14159 declares a constant Use only uppercase letters for the name (except for the prefix) Initialize the constant when declaring it (can’t do it anywhere else!) Can have the same types for constants as for variables 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
11
110-D11 Some common types We have seen: String, Integer, Double Also: Uses lots of memory, not as efficient. Avoid if you can Boolean For logical values (True/False) Dim blnLightOn As Boolean 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. Dim objAnything Dim objSomething As Object optional Decimal For decimal fractions such as dollars and cents Dim decMyBalance As Decimal
12
110-D12 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 Dim strName As String strName = "Jane Beckmann" a declaration an assignment statement an expression (anything that has a value) Note: to get " in a string, write "" strDialog = "She said ""Well,...""" to get She said "Well,..."
13
110-D13 Processing an assignment statement Consider: dblPerimeter = dblPI*dblDiameter 1 Evaluate the right hand side (=expression) dlbPI*dblDiameter Note: can have intYear = intYear + 1 (NOT a mathematical equation!) 2 The value is stored in the left hand side, the assignment variable dblPerimeter
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.