IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.

Slides:



Advertisements
Similar presentations
Variables, Constants &Computation T o i n s e r t y o u r c o m p a n y l o g o o n t h i s s l i d e F r o m t h e I n s e r t M n u e l e c t P i c.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1.
Chapter 3 Variables, Constants, and Calculations
Chapter 3.3 Numeric Data Types and Variables. Slide 2 Objectives Create variables to store information while a solution runs Perform computations with.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 04.
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Data Types 1.
BACS 287 Programming Fundamentals 1. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Variables State Scope Lifetime.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Variables and Constants
.NET Data types. Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Applications Development
Chapter 3 Variables, Constants and Calculations Programming In Visual Basic.NET.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Programming with Microsoft Visual Basic th Edition
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
VB.NET 2008 Introduction to Variables Part 1. Overview.NET Languages –Source Code –Compiler –MSIL –CLR & Windows Variables –Data Types –Converting.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
1.
A variable is a name for a value stored in memory.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Data Types, Arithmetic Operations
Lecture Set 4 Data Types and Variables
Variables and Arithmetic Operations
Chapter 6 Variables What is VBScript?
CIS16 Application Development Programming with Visual Basic
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Visual Basic Variables
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion

Learning Objectives data types - variables and constants - scope & lifetime - declaration (option explicit, option strict) assignment - numeric - string - date some useful functions & controls - type conversion

Frequently-used data types integer short 2 bytes -32,768 to +32,767 integer 4 bytes about +/- 2 billion long 8 bytes a very large integer number real decimal 16 bytes 0 to 28 digits of accuracy single 4 bytes 38 to 45 digits of accuracy double 8 bytes 308 to 324 digits of accuracy Boolean 2 bytes true or false string 2 bytes for each character up to 2 billion Unicode characters date 8 bytes object 4 bytes

Constants Named constants: (programmer defined) e.g., const pi as single = const taxRate as single =0.07 Intrinsic constants: (system provided) e.g., Color.White Color.Blue Color.Red Color.Yellow Color.Green...

Structure of a VB Application Project (s) Form(s) Controls solution

Scope of variables & constants dim i as integer const m as short = 8 module-level (form): variables declared using the Private declaration private y as integer private const j as integer = 5 local (sub): variables declared using the Dim declaration project level variables: variables declared using the Public declaration Public z as integer Public const k as integer = 15 block (within a loop) For i = 0 to 3 dim k as integer … Next i

Lifetime of Variables -local: only alive during the execution of a procedure or a block of code -For temporary results/values - form-level: alive as long as the form remains loaded (typically the whole lifetime of a project) -(namespace) project : project -If values are to be preserved after the project itself is not active -External data depositories

Declaration of Variables & Constants “option explicit on” as default to enforce declaration Why is declaration a good idea? - e.g., dim monument as integer rebates = …. rebtaes = rebates + 75 ‘ the error will be detected if declaration is mandatory …. local declaration - e.g., Private Sub command1_click(…) … dim x as integer dim y const MARKUP as single = 0.14 … End sub

Declaration of Variables & Constants form-level declaration where? In a declaration section of the form private x, y, z as integer private const rstmsg as string = “Please Reset all Values ” project-level declaration where? I t is good practice to declare them all in a single module. (show in class) public x as integer public const COMP_NAME as string = “First Trust and Bank” public const MAX_BONUS as decimal = 11500

Variable Declaration

Project-level Declaration

Structure of a VB Application Project (s) Form(s) Controls code module - declare public var. & constants - utility procedures such as sort, etc. solution

Choosing the variable scope Choose the variable and constant scope as narrow as possible minimizes computational needs of the code Makes code more modular, divided into useful, independent components

Recommended Naming Conventions prefix integer integer int long lng real decimal dec single sng double dbl Boolean bln string str date dat Scope Prefix project g module (form) m local none For example: gstrCustomerName

Assignment (numeric) operators + addition - subtraction * multiplication / division ^ exponentiation \ integer division 5 \ 2 = 2 mod modulus 5 mod 2 = 1 e.g., a = (5 * 4) / 2

Assignment (string variable) empty string - e.g., dim x as string x = ““ text1.text = x assign one string to another - e.g., text2.text = text1.text string concatenation using “&” - e.g., dim firstname, lastname as string firstname = “John” lastname = “Smith” label1.text = firstname & “ “ & lastname concatenate with itself - e.g., dim name as string name = “Roberts” name = name & “ Rogers” John Smith

Assignment (Date) e.g., dim date1, date2 as Date date2 = # :15# date2 = #March 6, :15 pm# date2 = #14:15# date1 = “1/20/97” with option strict off date1 = “2:12 am” with option strict off

Option Strict “Option Strict Off” is the default setting. Change the setting by typing “Option Strict On” at the top of the code window. With “Option Strict On”, the system strictly enforces data type checking.  Data conversion from “wider” to “narrower” data type is not allowed  Conversion between string and numeric types is not allowed  This prevent many hard to find bugs and execution errors

Option Strict With “Option Strict Off”, it is okay to write: Dim s as string, i as single s = “34” i = s / 2 With “Option Strict On”, the code above will generate an error message: Option Strict On disallows implicit conversion from string to single.

Widening & narrowing Option Strict Off Option Strict On Widening OK Narrowing OK, but may result in over- flow exception in runtime No Widening: store a variable value into another variable with greater precision or magnitude Narrowing: the reverse Dim i as integer Dim j as long i = 35 j = i Widening

Narrowing (Option Strict Off) Dim i As Integer ‘4-bytes Dim j As Long ‘8-bytes j = i = j The above code will generate a runtime exception, but not a warning in design time.

Data type conversion functions CStr string CInt integer CLng long CSng single CDbl double CDec decimal CDate date … Convert its argument toFunction Dim x as integer Dim s as string x = 5 s = CStr(x) s = “5” Dim d as date Dim s as string s = “01/15/2003” d = CDate(s) d = #01/15/2002#