Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.

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.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
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.
Chapter 3.3 Numeric Data Types and Variables. Slide 2 Objectives Create variables to store information while a solution runs Perform computations with.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Microsoft Visual Basic: Reloaded Chapter Three Memory Locations and Calculations.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Data Types 1.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 4 Introduction to Numeric Data Types and Variables.
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
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 3 Input, Variables, Constants, And Calculations.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 2: Chapter 3: Slide 1 Unit 2 Variables and Calculations Chapter 3 Input,
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Lecture 8 Visual Basic (2).
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Chapter 2: Using Data.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Applications Development
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
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.
Programming with Microsoft Visual Basic th Edition
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
VB.NET 2008 Introduction to Variables Part 1. Overview.NET Languages –Source Code –Compiler –MSIL –CLR & Windows Variables –Data Types –Converting.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Introduction to Programming Lecture 3 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic Fundamental Concepts
IS 350 Numeric Data Types.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
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
Data Types 1.
Variables and Arithmetic Operations
Chapter 3: Using Variables and Constants
Lecture Set 4 Data Types and Variables
Variables and Arithmetic Operations
Visual Basic..
Numbers.
String Variable, Methods and Properties
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
String Variable, Methods and Properties
String Variable, Methods and Properties
String Variable, Methods and Properties
Presentation transcript:

Data Types

Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store a floating-point value, but is more accurate than type Single. Type Single is useful in applications that need to conserve memory and do not require the accuracy provided by type Double. Single or Double ?

Declaring a Variable Dim statement declares variable Dim varname As type [ = initexpr ] is variable name – varname is variable name – Ascontains data type of variable – As type contains data type of variable – Optionalcontains the initial value of a variable – Optional initexpr contains the initial value of a variable

Declaring a Variable when declaring it Initialize a variable when declaring it – New to VB.NET Declare an Integer variable and store the value 30 in the variable Declare an Integer variable and store the value 30 in the variable Dim mintYearTerm As Integer = 30

Declaring a Variable Possible to declare multiple variables on the same line Dim mintYearTerm, mintMonthTerm As Integer

Dim dExpiration As Date dExpiration = #1/1/2003# Dim dnewExpiration As Date dnewExpiration = dExpiration. AddYears(3) Declaring a Variable

Declaring Constants Declare constants with the Const statement. Constants are similar to variables – constants are values that are stored to memory locations; however, a constant cannot have its value change during program execution – constant values are generally fixed over time. Examples: – Const BIG_STATE_NAME_STRING As String = "Alaska" – Const MAX_SIZE_INTEGER As Integer = 4000

Strings quote1 = “ The ball game isn’t over “ quote2 = “ until it is over “ quote = quote1 & “, “ & quote2 console.writeLine ( quote )  The ball game isn’t over, until it is over

Dim firstName, secondName, fullName As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click firstName = TextBox1.Text secondName = TextBox2.Text fullName = firstName & " " & secondName TextBox3.Text = fullName End Sub Strings- Example

Examining Variable Types IsNumeric() IsDate() System.Date.IsLeapYear (2001)  False

Boolean Data Type It stores True (1) / False (0) Any non-zero value will be considered as TRUE. These variables are combined with the logical operators.. AND, OR & NOT.

Converting Input Data Types Text property always stores string values, even if the string looks like a number. Parse method – converts a string value to an equivalent numeric value for storage to a numeric variable. Parse means to examine a string character by character and convert the value to another format such as decimal, integer, or string. Dim QuantityInteger As Integer = Integer.Parse(TextBox2.Text)

Converting Output Data Types In order to display numeric variable values as output the values must be converted from numeric data types to string in order to store the data to the Text property of a TextBox control. Use the ToString method. TextBox2.Text = QuantityInteger.ToString()

Conversion Between Data Types 1. Methods belong to the System.Convert class – ToInt16 converts value to a Short – ToInt32 converts value to an Integer – ToInt64 converts value to a Long – ToDouble converts value to a Double – ToSingle converts value to a Single – ToString converts value to a String

Dim sngInput As Single = 3.44 Dim strInput As String = "3.95" Dim intOutput As Integer Dim sngOutput As Single Convert a Single to an Integer intOutput = System.Convert.ToInt32(sngInput) Convert a String to an Integer intOutput = System.Convert.ToInt32(strInput) Convert a String to a Single sngOutput = System.Convert.ToSingle(strInput) Conversion Between Data Types

2. CType and named functions: CType method Dim A As string = “34.56” Dim B As Double B = CType ( A, Double) / 1.14 Older versions of VB used named functions to convert values. Examples are the CDec (convert to Decimal) and CInt (convert to Integer): PriceDecimal = CDec(TextBox1.Text) QuantityInteger = CInt(TextBox2.Text) CDate, CDec, CStr, CLng, CChar, CBool, CByte Conversion Between Data Types

Implicit Conversion Implicit Conversion – this is conversion by VB from a narrower to wider data type (less memory to more memory) – this is done automatically as there is no danger of losing any precision. In this example, an integer (4 bytes) is converted to a double (8 bytes): BiggerNumberDouble = SmallerNumberInteger

Visual Basic provides several options for controlling the way the compiler handles data types. These options can help programmers eliminate such errors as those caused by narrowing conversions, making code more reliable and secure. Option Strict and Data-Type Conversions Option Strict and Data-Type Conversions

Option Explicit – Set to On by default – Forces the programmer to declare explicitly all variables before they are used Option strict – Set to Off by default – When set to On, it forces the programmer to perform an explicit conversion for all narrowing conversions

Option Strict can be activated through the IDE by right-clicking the project name in the Solution Explorer. From the resulting menu, select Properties to open the Property Pages dialog. From the directory tree on the left side of the dialog, select Build from the Common Properties list. In the middle of the dialog is a drop-down box labeled Option Strict:. By default, the option is set to Off. Choose On from the dropdown box and press Apply. Option Strict and Data-Type Conversions Option Strict and Data-Type Conversions

Conversion – Summary Rules Use the Parse method to convert a string to a number or to parse the value in a textbox control. Use the Convert method to convert a type of number to a different type of number