Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
1.
VBA Modules, Functions, Variables, and Constants
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
1 Pertemuan 03 Representing Data Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,
VB .NET Programming Fundamentals
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Variables and Constants
Chapter 3: Using Variables and Constants
Tutorial 11 Using and Writing Visual Basic for Applications Code
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
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.
Lecture 8 Visual Basic (2).
Dani Vainstein1 VBScript Session 1. Dani Vainstein2 Subjets for Session 1 Vbscript fundamentals. Variant subtypes. Variables. Option Explicit statement.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
INT213-Week-2 Working with Variables. What is variable
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Visual Basic Programming I 56:150 Information System Design.
# 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?
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming with Microsoft Visual Basic th Edition
Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
A variable is a name for a value stored in memory.
VBScript Session 1 Dani Vainstein.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
VBScript Session 1 IT Professional Academy.
Data Types, Identifiers, and Expressions
Computing with C# and the .NET Framework
2. Understanding VB Variables
Lecture Set 4 Data Types and Variables
Chapter 6 Variables What is VBScript?
Data Types, Identifiers, and Expressions
CIS16 Application Development Programming with Visual Basic
PHP.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Intro to Programming Concepts
Predefined Functions Revisited
VBScript Session 1.
Chap 2. Identifiers, Keywords, and Types
Quick Test Professional Training Part 1
Review of Java Fundamentals
Presentation transcript:

Slide 1 Variables, Constants and Data Types

Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value) v Variable name must: –Start with a letter –No spaces, periods nor punctuation characters –Unique (within its scope) –255 characters in length –Not a VB reserved word

Slide 3 Variables v Variable naming conventions: –Descriptive (code is easy to read) –Short as possible (easy to type) –Use a prefix for type (easy for programmer to know its type) u String ssName u IntegernnAge u (table 8.1, page 161)

Slide 4 Types of Variables v Integer2 bytes to v Long4+/-2 billion v Single4+/-1E-45 to 4E38 v Double8+/-5E-324 to 1.8E308 v Currency8+/- 9E14 v String1B/char65000 fixed, 2 billion dynamic v Byte10 to 255 v Boolean2True or False v Date81/1/100 to 12/31/999 v Object4 v Varient16B+1B/Char

Slide 5 Variable Declaration v If a variable is not declared, it is declared by default by VB as Variant –Waste memory resources –Type might be invalid for use with functions v It is good programming practice to declare variables

Slide 6 Explicit Declaration v Explicit declaration uses statements to define the names and types of variables –Dim sFname as String, sLname as String –Private sFname as String, sLname as String –Static sFname as String, sLname as String –Public sFname as String, sLname as String v Multiple variables with multiple types v If type is not indicated, Variant will be used

Slide 7 Implicit Declaration v VB defines the variable the first time it is used v Variable name is preceded with a special character: –Integer% –Long& –Single! –Double# –String$ v Example: – nNumVal% = 0 –sFirstName$ = “Ali”

Slide 8 Fixed-Length Strings v Strings in general are variable-length v Fixed length strings are declared like: –Dim sName As String * 25 v If a shorter string is assigned, it is appended with blanks v If a longer string is assigned, it is truncated

Slide 9 Variable Array v Array: is a group of variables of the same type, sharing the same name v Example: Dim nScores (1 to 35) As Integer v Use for loop with array For nCounter = 1 to 20 nScores(nCounter) = 0 Next nCounter

Slide 10 Scope v By default variables are local to the procedures where they are created in (local variable) v Public variables: can be accessed anywhere at the code v Example: Public sUserName As String (Defined in the General Declarations Section) v Using Public variables: –Hard to debug –Bad use of resources

Slide 11 Static Variables v Variables declared local to a procedure are discarded afterwards v To preserve a variable in side a procedure use Static v Static nPages As Integer v Using Static with functions or sub treats all variables inside as Static

Slide 12 Option Explicit v Setting “REqire Variable Declaration” option (Tools – Options – Editor) v It places Option Explicit in the general section v has no effect on Forms/Modules created before setting it

Slide 13 Constants v Cannot be modified v Easy to remember then it value v Avoid typing long strings v Minimize program modifications v Examples: –Const vbActibeTitleBar = –Const PI = –Public Const PI =

Slide 14 Converting Data Types v VB provides several conversion functions you can use to convert values into a specific data type. v To convert a value to Currency, for example, you use the CCur function: PayPerWeek = CCur(hours * hourlyPay)

Slide 15 The Empty Value v Sometimes you need to know if a value has ever been assigned to a created variable. A Variant variable has the Empty value before it is assigned a value. The Empty value is a special value different from 0, a zero-length string (""), or the Null value. You can test for the Empty value with the IsEmpty function: –If IsEmpty(Z) Then Z = 0 v When a Variant contains the Empty value, you can use it in expressions; it is treated as either 0 or a zero-length string, depending on the expression. v The Empty value disappears as soon as any value (including 0, a zero-length string, or Null) is assigned to a Variant variable. You can set a Variant variable back to Empty by assigning the keyword Empty to the Variant.

Slide 16 The Null Value v The Variant data type can contain another special value: Null. v Null is commonly used in database applications to indicate unknown or missing data. Because of the way it is used in databases, Null has some unique characteristics: v Expressions involving Null always result in Null. Thus, Null is said to "propagate" through expressions; if any part of the expression evaluates to Null, the entire expression evaluates to Null. v Passing Null, a Variant containing Null, or an expression that evaluates to Null as an argument to most functions causes the function to return Null.

Slide 17 The Null Value v You can also assign Null with the Null keyword: –Z = Null v You can use the IsNull function to test if a Variant variable contains Null: If IsNull(X) And IsNull(Y) Then Z = Null Else Z = 0 End If

Slide 18 Built-in Constants v Intrinsic Constants v Colors, keycodes, shapes v Described in help v Object Browser