Download presentation
Presentation is loading. Please wait.
Published byHilda Miles Modified over 8 years ago
1
Data Types
3
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 ?
4
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
5
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
6
Declaring a Variable Possible to declare multiple variables on the same line Dim mintYearTerm, mintMonthTerm As Integer
7
Dim dExpiration As Date dExpiration = #1/1/2003# Dim dnewExpiration As Date dnewExpiration = dExpiration. AddYears(3) Declaring a Variable
8
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
9
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
10
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
11
Examining Variable Types IsNumeric() IsDate() System.Date.IsLeapYear (2001) False
12
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.
13
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)
14
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()
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.