CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.

Slides:



Advertisements
Similar presentations
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
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.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
VBA Modules, Functions, Variables, and Constants
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
VB .NET Programming Fundamentals
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.
.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.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
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.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
CPS120: Introduction to Computer Science
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
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.
Applications Development
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.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
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.
 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
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CPS120: Introduction to Computer Science Variables and Constants.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT.
Controlling Program Flow with Decision Structures.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
Week 2 - Wednesday CS 121.
A variable is a name for a value stored in memory.
Data Types, Arithmetic Operations
Variables and Primative Types
2. Understanding VB Variables
Chapter 6 Variables What is VBScript?
Numbers.
CIS16 Application Development Programming with Visual Basic
Variables Title slide variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Tonga Institute of Higher Education
CHAPTER FOUR VARIABLES AND CONSTANTS
VB Variables and Data
Introduction to Primitives
Introduction to Primitives
Unit 3: Variables in Java
B065: PROGRAMMING Variables 2.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Variables and Constants
Presentation transcript:

CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011

Longest Book? How many words are there in the longest book? How many characters are there in a word, on average (including the space following it)? How many copies of the longest book could go into one VB character variable?

Variables a variable has the following: name (looks up its location in memory)  must start with a letter (not a number) data type e.g., various types of numbers, character, boolean, etc. value (stored in the memory location) the value can change (it can vary) "variable cloudiness"

Variable Naming Issues advantages of a convention e.g. txtAge makes code easier to debug and maintain? naming objects makes it easier for Prof. Westfall to grade projects (this is a hint!) disadvantages of a convention extra work (at 1 st, but less debugging later?)

Integer VB.NET Variable TypesVB.NET Variable Types Byte – unsigned integer: 0 to 255 Char – unsigned integer: 0 to 65,535 (Unicode) Short – signed, same as Int16 –32768 to Integer – signed, same as Int32 –2 billion to + 2 billion Long Int64 – signed, same as Int64 –9 quintillion to +9 quintillion need to know that these data types are whole numbers (integers means no decimal points)

Decimal VB.NET Variable TypesVB.NET Variable Types Single: 4 bytes, 6-digit accuracy Double: 8 bytes, 14-digit accuracy can have 300+ zeros before/after decimal Decimal: 12 bytes, 28-digit accuracy (fixed decimal point, data never rounds) can have <= 29 digits before decimal point or 28 zeros/digits after the decimal point know which data types are decimals see notes

Other VB.NET Variable Types String: "Lee" 2 billion character maximum string size! ? Char – Unicode character (2 bytes) integer value that identifies character Boolean: True or False (4 bytes!) Date: 1 – 9999 AD (or CE) time and date stored in a variable, can extract or display the part(s) you want Variants do not exist in.NET can use Objects instead for whatever 'code can use Objects instead

Variable Naming Conventions in some programming languages, it is common to prefix the data type (1 - 3 chars) to the front of the variable name also known as "Hungarian notation""Hungarian notation" used to be popular at Microsoft not popular at all with Java programmers  cultural issue between Microsoft and rest of world? for objects, Prof. Westfall still likes it

Naming Class Variables formerly used naming convention for member (class or module) variables was to put an m (for module) in front of the other prefix e.g., mals (or m_als) for an arraylist most of recent sources recommend against this

Declaring Variables Explicitly Dim vname [as vartype] [,vname2 ] as vartype  vartype is required if Option Strict is On (otherwise defaults based on the values assigned to the variables) can declare multiple variables with one Dim but Prof. W really dislikes it (  hint) Dim nI, nJ, nK as Integer '  * Dim nI as Integer, nK as Integer '  Dim nI as Integer, dJ as Double '  '* 1-letter naming convention

Declaring String Length could declare a String as a specified length in VB 6 Dim sName as String * 20 unused space gets filled with spaces sName ="Joe" becomes "Joe " length is actual String length in.NET.PadRight() function adds trailing spaces  "Joe".PadRight(20) 'to get 20 characters

Declaring Variables Implicitly can declare without types if Option Strict is Off (not On) 'but let's NOT do this  nNumber = 0 'default based on type/size can also declare with letters or special characters (but this is kind of confusing) decCash = D (Decimal) 'need to use this in decimal calculations lngPop& =1000 or lngPop = 1000L (Long ) dblRate#=.05 (Double) strChar$ = "Z" (String) etc.

Constants value is "hard coded" program can't change the value Const TAX_BASE_RATE =.15 Visual Basic "intrinsic constants" numeric values – make something happen  easier to remember than numbers  use vbOKOnly for message box style actually vbOKOnly = 0

Variable Scope - Public Public varname as vartype Public replaces Dim can't declare as Public inside a sub can be used by code outside this file (has namespace scope) disadvantages uses more memory can make code harder to maintain  may be hard to locate its declaration

Variable Scope - Module Dim varname [as vartype] in general declarations (above/outside of procedures) can only be used by code in this file or class/module advantage: easier to maintain can use Private instead of Dim outside of procedures same effect

Variable Scope - Procedure Dim varname [as vartype] can declare with Dim, but not Public inside a procedure (Sub or Function) accessible only in the procedure itself, including in its blocks (next slide) value is lost when procedure terminates

Variable Scope - Block declared within control structures selection, looping, try/catch only accessible inside the block value is lost when execution exits the block if want to use it later, declare it outside of the block (e.g., outside of a loop)

Variable Scope - Static Static varname [as vartype] varname doesn't lose its value when its procedure terminates in a program example: in a print subroutine, a static variable can save the page number adds 1 to the static value when procedure prints another page in current report value stored while other procedures run

Procedures Have Scope Too Public Function MyFunction Private Sub MySub public Subs can be run by other files without Private, a Sub defaults to Public variables in a procedure must be declared with Dim (are private) a Private procedure can still set variables in general declarations (outside of itself) 'notes

Option Explicit On requires you to declare all variables helps spot spelling errors during development removing Option Explicit might improve efficiency when project goes into production can set in Visual Studio for all future projects Tools>Options>Projects and Solutions>VB Defaults>Option Explicit>On or can type at top of a previously created code file: Option Explicit On

Option Strict On automatically turns Option Explicit on forces you to also include the data type in a declaration also have to use conversion functions (e.g., CInt, CStr, etc.) when assigning a value to a variable of a different type can set in VS for all future projects Tools>Options>Projects and Solutions>VB Defaults>Option Explicit>On