data types. visual basic provides data type single for storing single-precision floating-point...
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 variableDim varname As type [ = initexpr ]–varname is variable name–As type contains data type of variable–Optional initexpr contains the initial
value of a variable
Declaring a Variable
• Initialize a variable when declaring it–New to VB .NET
• 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
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 SALES_TAX_RATE_SINGLE As Single = 0.0725F– Const BIG_STATE_NAME_STRING As String = "Alaska"– Const TITLE_STRING As String = "Data Entry Error"– Const MAX_SIZE_INTEGER As Integer = 4000
• Follow these rules for assigning numeric values to constants:• You can use numbers, a decimal point, and a plus (+) or minus
(-) sign.• Do not include special characters such as a comma, dollar
sign, or other special characters. • Append one of these characters to the end of the numeric
constant or variable to denote a data type declaration. If you do not use these, a whole number is assumed to be Integer and a fractional value is assumed to be Double.
• Decimal D 40.45D• Double R 12576.877R• Integer I 47852I• Long L 9888444222L• Short S 2588S• Single F 0.0725F
Declaring Constants
Strings
• Such as sentences, words, letters of the alphabet, names, telephone numbers& addresses.• A string constant is a sequence of
characters that is treated as a single item.
Strings
quote1 = “ The ball game isn’t over “ quote2 = “ until it is over “
quote = quote1 & “ , “ & quote2Console.WriteLine ( quote )
The ball game isn’t over , until it is over
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)
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 msngInput As Single = 3.44Dim mstrInput As String = "3.95"Dim mintOutput As IntegerDim msngOutput As Single
• Convert a Single to an IntegermintOutput = System.Convert.ToInt32(msngInput)
• Convert a String to an IntegermintOutput = System.Convert.ToInt32(mstrInput)
• Convert a String to a SinglemsngOutput = System.Convert.ToSingle(mstrInput)
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