Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1. The built-in value types (continued)

Similar documents
Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Outline. Data and Operations. Data Types. Integral Types

Data Types. Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions

Programming Language 2 (PL2)

Data and Operations. Outline

Chapter 2: Using Data

3.1. Chapter. CSC 252 (MIS 385) Chapter 3 Input, Variables, Exceptions, and Calculations. Introduction. Page 1

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Microsoft Visual Basic 2005: Reloaded

Declaration and Memory

Microsoft Visual Basic 2015: Reloaded

VB FUNCTIONS AND OPERATORS

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

Visual C# Instructor s Manual Table of Contents

Ex: If you use a program to record sales, you will want to remember data:

VB.NET MOCK TEST VB.NET MOCK TEST I

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

COMP Primitive and Class Types. Yi Hong May 14, 2015

Programming in C++ 6. Floating point data types

Agenda & Reading. VB.NET Programming. Data Types. COMPSCI 280 S1 Applications Programming. Programming Fundamentals

CS313D: ADVANCED PROGRAMMING LANGUAGE

Advanced Computer Programming

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Understand Computer Storage and Data Types

Eng. Mohammed S. Abdualal

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

1.3b Type Conversion

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Bit (0, 1) Byte (8 bits: 0-255) Numeral systems. Binary (bin) 0,1 Decimal (dec) 0, 1, 2, 3, Hexadecimal (hex) 0, 1, 2, 3, 1/13/2011

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Question And Answer.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 2: Using Data

COMP2611: Computer Organization. Data Representation

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Computer System and programming in C

Number Representation & Conversion

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Values, Variables, Types & Arithmetic Expressions. Agenda

Introduction to C# Applications

Full file at

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Values and Variables 1 / 30

Values & Variables 1

BASIC ELEMENTS OF A COMPUTER PROGRAM

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Object Oriented Programming with Visual Basic.Net

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Reserved Words and Identifiers

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Chapter 3 Structured Program Development in C Part II

More Programming Constructs -- Introduction

Integers II. CSE 351 Autumn 2018

CIS 110: Introduction to Computer Programming

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

2. A GUI A. uses buttons, menus, and icons B. should be easy for a user to manipulate C. both (a) and (b) D. stands for Graphic Use Interaction

Java Primer 1: Types, Classes and Operators

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Data Types & Variables

CIS133J. Working with Numbers in Java

Visual Basic 2008 Anne Boehm

ECE 122 Engineering Problem Solving with Java

Operators and Expressions

.Net Technologies. Components of.net Framework

HOW TO DEVELOP A VB APPLICATION

Program Fundamentals

Java Notes. 10th ICSE. Saravanan Ganesh

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

Fundamentals of Programming

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2: Using Data

Unit 3. Constants and Expressions

CIS 3260 Intro to Programming with C#

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

JAVA Programming Fundamentals

Chapter 2: Data and Expressions

Computational Expression

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

CHAPTER 3 Expressions, Functions, Output

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 1 Getting Started

Data Types Literals, Variables & Constants

DigiPen Institute of Technology

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Visual Basic for Applications

3. Java - Language Constructs I

Darshan Institute of Engineering & Technology for Diploma Studies

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

ESc101 : Fundamental of Computing

Vba Variables Constant and Data types in Excel

Transcription:

The built-in value types Keyword Bytes Type Description Byte 1 Byte Positive integer value from 0 to 255 SByte 1 SByte Signed integer value from -128 to 127 Short 2 Int16 Integer from 32,768 to +32,767 UShort 2 UInt16 Unsigned integer from 0 to 65,535 Integer 4 Int32 Integer from 2,147,483,648 to +2,147,483,647 UInteger 4 UInt32 Unsigned integer from 0 to 4,294,967,295 Long 8 Int64 An integer from 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1 The built-in value types (continued) Keyword Bytes Type Description ULong 8 UInt64 An unsigned integer from 0 to +18,446,744,073,709,551,615 Single 4 Single A non-integer number with approximately 7 significant digits Double 8 Double A non-integer number with approximately 14 significant digits Decimal 16 Decimal A non-integer number with up to 28 significant digits (integer and fraction) that can represent values up to 7.9228 x 10 28 Char 2 Char A single Unicode character Boolean 1 Boolean A True or False value Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 2 1

Arithmetic operators Operator Name + Addition - Subtraction * Multiplication / Division \ Integer division Mod Modulus ^ Exponentiation + Positive sign - Negative sign Assignment operators Operator Name = Assignment += Addition -= Subtraction *= Multiplication /= Division \= Integer division Precedence/Order of Operations 1. Exponentiation 2. Positive and negative 3. Multiplication and division 4. Integer division 5. Modulus 6. Addition and subtraction Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 3 How implicit casting works Widening conversions Byte Short Integer Long Decimal Integer Double Short Single Double Char Integer Implicit casts for widening conversions Dim grade As Double = 93 ' convert Integer to Double Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10 Dim average As Double = (a + b + c) / 3 ' convert b and c to Doubles ' average = 7.5 (22.5 / 3) Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 4 2

How implicit casting works (continued) Implicit casts for narrowing conversions Dim grade As Integer = 93.75 ' grade = 94 (rounded) Dim subtotal As Decimal = txtsubtotal.text ' may throw an exception Dim average As Integer = (a + b + c) / 3 ' average = 8 By default, Visual Basic uses permissive type semantics. That means that narrowing conversions can be done implicitly. If you change to strict type semantics, narrowing conversions must be done explicitly. Option Strict On Option Explicit On How to code explicit casts with the CInt and CDec functions Dim grade As Integer = CInt(93.75) ' CInt converts to Integer with rounding ' (grade = 94) Dim subtotal As Decimal = CDec(txtSubtotal.Text) ' CDec converts to Decimal ' may throw an exception Dim average As Integer = CInt((CInt(a) + b + c) / 3) ' convert a and the calculated result to Integer values; ' average = 7 Murach s Visual Basic 2010, 2012, C4 2010, 2013, Mike Murach & Associates, Inc. Slide 5 Slide 5 Common.NET structures that define value types Structure VB keyword The value type holds Byte Byte An 8-bit unsigned integer Int16 Short A 16-bit signed integer Int32 Integer A 32-bit signed integer Int64 Long A 64-bit signed integer Single Single A single-precision floating-point number Double Double A double-precision floating-point number Decimal Decimal A 96-bit decimal value Boolean Boolean A True or False value Char Char A single character Common.NET classes that define reference types Class name VB keyword The reference type holds String String A reference to a String object Object Object A reference to any type of object Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 6 3

Some of the Visual Basic functions for data conversion Function CDbl(expression) CDec(expression) CInt(expression) CLng(expression) CStr(expression) Converts the expression to the Double data type Decimal data type Integer data type; any fractional portion is rounded to the nearest whole number using banker s rounding Long data type String type CType(expression, Typename) Specified type, which can be any type of object Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 7 Examples of the data conversion functions Dim grade As Integer = CInt(93.75) ' grade = 94 Dim subtotal As Decimal = CDec(txtSubtotal.Text) ' may throw an exception Dim a As Double = 6.5 Dim b As Integer = 6 Dim c As Integer = 10 Dim average As Integer = CInt((CInt(a) + b + c) / 3) ' average = 7 Dim salesstring As String = "$2,574.98" Dim salesdecimal As Decimal = CDec(salesString) ' CDec accepts $, and. Dim salesstring = CStr(salesDecimal) ' salesstring value = 2574.98 Dim salesstring = CType(salesDecimal, String) ' same result as CStr Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 8 4

Common methods for data conversion Method ToString([format]) Parse(string) TryParse(string, result) Description A method that converts the value to its equivalent string representation using the specified format. If the format is omitted, the value isn t formatted. A shared method that converts the specified string to an equivalent data value. If the string can t be converted, an exception occurs. A static method that converts the specified string to an equivalent data value and stores it in the result variable. Returns a True value if the string is converted. Otherwise, returns a False value. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 9 Conversion statements that use ToString, Parse, and TryParse Dim sales As Decimal = 2574.98D Dim salesstring As String = sales.tostring ' decimal to string sales = Decimal.Parse(salesString) ' string to decimal Decimal.TryParse(salesString, sales) ' string to decimal An implicit call of the ToString method Dim price As Decimal = 49.5D Dim pricestring As String = "Price: $" & price ' automatic ToString call A TryParse method that handles invalid data Dim salesstring As String = "$2574.98" Dim sales As Decimal = 0 Decimal.TryParse(salesString, sales) ' sales is 0 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 10 5

Some of the shared methods of the Convert class Method ToDecimal(value) ToDouble(value) ToInt32(value) ToChar(value) ToBool(value) ToString(value) Description Converts the value to the Decimal data type. Converts the value to the Double data type. Converts the value to the Integer data type. Converts the value to the Char data type. Converts the value to the Boolean data type. Converts the value to a String object. Conversion statements that use the Convert class Dim subtotal As Decimal subtotal = Convert.ToDecimal(txtSubtotal.Text) ' string to decimal Dim years As Integer = Convert.ToInt32(txtYears.Text) ' string to integer txtsubtotal.text = Convert.ToString(subtotal) ' decimal to string Dim subtotalint As Integer = Convert.ToInt32(subtotal) ' decimal to integer Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 11 The basic syntax of three of the formatting functions FormatNumber(expression [, numdigitsafterdecimal]) FormatCurrency(expression [, numdigitsafterdecimal]) FormatPercent(expression [, numdigitsafterdecimal]) Statements that use the formatting functions txtdiscountamount.text = FormatNumber(discountAmount, 2) txttotal.text = FormatNumber(invoiceTotal) txttotal.text = FormatCurrency(invoiceTotal) txtdiscountpercent.text = FormatPercent(discountPct) Results of the three formatting functions Expression Result FormatNumber(-.888) -0.89 FormatNumber(-.888, 2) -0.89 FormatNumber(-.888, 1) -0.9 FormatNumber(3000) 3,000.00 FormatCurrency(3000) $3,000.00 FormatPercent(-.888) -88.80% FormatPercent(-.888, 1) -88.8% Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 12 6

Standard numeric formatting codes Code C or c P or p N or n F or f D or d E or e G or g Format Currency Percent Number Float Digits Exponential General How to use the ToString method to format a number Dim monthlyamount As String = amount.tostring("c") ' Example: $1,547.20 Dim interestrate As String = interest.tostring("p1") ' Example: 2.3% Dim quantitystring As String = quantity.tostring("n0") ' Example: 15,000 Dim paymentstring As String = payment.tostring("f3") ' Example: 432.818 Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 13 Extra 4-1 Calculate area and perimeter In this exercise, you ll create a form that accepts the length and width of a rectangle from the user and then calculates the area and perimeter of the rectangle. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 14 7

Extra 4-2 Accumulate test score data In this exercise, you ll create a form that accepts one or more scores from the user. Each time a score is added, the score total, score count, and average score are calculated and displayed. Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 15 8