Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz.

Similar presentations


Presentation on theme: "Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz."— Presentation transcript:

1 Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

2 Feb 23, 2007Dammam Community College2 Objectives Data Type Summary Variables and Assignments Strings Concatenation Constants and Char Data Types Integer and Real Number Data Types Date Time Data Type Default Values of Data Types

3 Feb 23, 2007Dammam Community College3 Data Type Summary The table on next slides shows: –Visual Basic.NET data types, –their supporting common language runtime types, –their nominal storage allocation, –and their value ranges.

4 Feb 23, 2007Dammam Community College4 Data Type Summary Visual Basic type Common language runtime type structure Nominal storage allocation Value range BooleanSystem.Boolean2 bytesTrue or False. ByteSystem.Byte1 byte0 through 255 (unsigned). CharSystem.Char2 bytes0 through 65535 (unsigned). DateSystem.DateTime8 bytes0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999. Page 138

5 Feb 23, 2007Dammam Community College5 Data Type Summary Visual Basic type Common language runtime type structure Nominal storage allocation Value range DecimalSystem.Decimal16 bytes0 through +/- 9,228,162,514,264,337,593,543, 950,335 with no decimal point; 0 through +/- 7.922816251426433759354395 0335 with 28 places to the right of the decimal; smallest nonzero number is +/- 0.000000000000000000000000 0001 (+/-1E-28).

6 Feb 23, 2007Dammam Community College6 Data Type Summary Visual Basic type Common language runtime type structure Nominal storage allocation Value range DoubleSystem.Double8 bytes-1.79769313486231570E+308 through (double- precision floating- point) -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values. IntegerSystem.Int324 bytes-2,147,483,648 through 2,147,483,647.

7 Feb 23, 2007Dammam Community College7 Data Type Summary Visual Basic type Common language runtime type structure Nominal storage allocation Value range LongSystem.Int648 bytes-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. (long integer) ObjectSystem.Object (class) 4 bytesAny type can be stored in a variable of type Object. ShortSystem.Int162 bytes-32,768 through 32,767.

8 Feb 23, 2007Dammam Community College8 Data Type Summary Visual Basic type Common language runtime type structure Nominal storage allocation Value range Single (single- precision floating- point) System.Single4 bytes-3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values. StringSystem.String (class) Depends on implementing platform 0 to approximately 2 billion Unicode characters.

9 Feb 23, 2007Dammam Community College9 Data Types ??? A reference type contains a pointer to another memory location that holds the data. –Strings, classes, arrays, collections, and objects A data type is a value type if it holds the data within its own memory allocation. –Referred to as primitive types or structures –Stores the actual data in the stack –Boolean and Char and Date –All numeric data types

10 Feb 23, 2007Dammam Community College10 Using Variables to Store Information “A variable is a temporary storage location for data in your program.” (see p. 127) A variable holds a value. There are many different kinds of values: numbers, letters, names, properties, etc. A variable has a name. A variable has an address (location in memory). A variable must be declared before it can be used.

11 Feb 23, 2007Dammam Community College11 It must Begin with a letter It may have a letter [A-Z], [a-z],[0-9] and '_' length in unlimited Do not use a digit in start No VB.NET reserved word can be used as variable name Do not use a period or space Avoid special characters except underscore First letter of each word is usually capitalized VB.NET commands and variable are NOT case sensitive It must be unique within its scope Variables Rules

12 Feb 23, 2007Dammam Community College12 Selecting a Name for a Variable (continued) Figure 3-4: Rules for variable names along with examples of valid and invalid names

13 Feb 23, 2007Dammam Community College13 Identifiers An identifier is a name for an object (variable, function, etc.) in a program. Note: the following rules are for the C language: Identifier must be made of letters, digits, and the underscore ‘_’, but cannot start with a digit. They can be any length. Cannot be the same as a reserved word (such as if, end, etc.). Example –a, player1, Player1, STACK_SIZE are legal. –1player, ten%, stack size, double are illegal.

14 Feb 23, 2007Dammam Community College14 Variable Declarations A variable must be declared before it can be used. Below is the syntax for a variable declaration in VB: Dim variableName As variableType “Dim” (short for dimension) and “As” are keywords and must appear as-is. variableName is the name of the new variable and is defined by the programmer. variableType must be one of the valid variable types. For example: Dim LastName As String

15 Feb 23, 2007Dammam Community College15 Variable Declarations Purpose: A variable declaration reserves space for the variable in memory for use, as the program runs. Where: The declaration for a particular variable must happen before the first use of the variable in the program. By convention, VB programmers declare all variables together first inside a procedure, before the first executable statement.

16 Feb 23, 2007Dammam Community College16 Variable Declaration (continued) First Three Characters Identify the Data Type Data TypePrefixSample Variable Name BooleanBlnBlnMember ByteBytBytZero CharChrChrLetter DateDatDatBirthdate DoubleDblDblWeight DecimalDecDecProductPrice IntegerIntIntNumberProducts LongLngLngSalary SingleSngSngAverage ShortShoShoYears StringStrStrLastName

17 Feb 23, 2007Dammam Community College17 ' compiler generates error, need to declare first Intnum = 100 ' max value of int is 2147483647 i.e 2 31 -1 Dim IntNum As Integer = 100// OK ' min value of int is -2147483648 i.e -2 31 Dim IntNum As Integer //OK IntNum = 100 //OK Variables and Assignments

18 Feb 23, 2007Dammam Community College18 Module Module1 Sub Main() Dim SngPrice As Single SngPrice=45.35 System.Console.Write ("This price is ") System.Console.WriteLine (SngPrice) End Sub End Module Output of this application is: The price is 45.35 Variables and Assignments

19 Feb 23, 2007Dammam Community College19 Variables ( Example ) Imports System.Console Module Module1 Sub Main() Dim Chrc As Char = "X" Dim Shos As Short= -32768 Dim Dbld As Double= 12.1234567890123 WriteLine ("char is " + Chrc) 'char is X Write ("short is " ) WriteLine ( Shos )'short is -32768 Write ("double is ") WriteLine (Dbld) 'double is 12.1234567890123 End Sub End Module

20 Feb 23, 2007Dammam Community College20 Write a program that declares one integer variable called num. Give this variable the value 1000 and then, using one WriteLine() statement, display the value on the screen like this: 1000 is the value of num Exercise (Variables and Assignments)

21 Feb 23, 2007Dammam Community College21 Strings Imports Microsoft.VisualBasic Module Module1 Sub Main() Dim Str1, StrFirst, StrLast, StrMid As String Str1 = "Mid Function Demo" StrFirst = Mid (Str1, 1, 3) ' "Mid". StrLast = Mid (Str1, 14, 4) ' "Demo". StrMid = Mid (Str1, 5)' "Function Demo". System.Console.Write (Len(Str1)) '17 System.Console.Write (StrFirst) ' Mid System.Console.Write (StrLast) ' Demo System.Console.Write (StrMid) ' Function Demo End Sub End Module

22 Feb 23, 2007Dammam Community College22 Strings Imports System.Console Module Module1 Sub Main() Dim StrFirst, StrLast As String StrFirst = "Tariq" StrLast = "Aziz" WriteLine (StrFirst + StrLast) 'TariqAziz WriteLine (StrFirst & StrLast) 'TariqAziz WriteLine (StrFirst & " " & StrLast) 'Tariq Aziz End Sub End Module

23 Feb 23, 2007Dammam Community College23 Assignment statements An assignment statement is an executable statement that results in storing a value in a variable. It is not an algebraic equation! Syntax: variable = expression The assignment operator is the equal sign (=) is the statement terminator. The lvalue is an expression that evaluates to a memory location. The rvalue can be a constant, variable, or complex combination including operators.

24 Feb 23, 2007Dammam Community College24 Assignment Statement Examples Dim x As Integer Dim y As Integer Dim z As Integer x = 1 y = 2 z = x + y x = x + 1 –What is the value of x after the code above executes? –Note: Sequence, selection, and repetition –See also page 130 lvalue rvalue

25 Feb 23, 2007Dammam Community College25 Assignment Statement Examples –Remember Visual Basic.NET is NOT case sensitive. However, Visual Studio.NET will place Dim, As, and String in upper & lower case characters Dim StrLastName, StrFirstName As String Dim StrStoreName As String = " Tara Store "

26 Feb 23, 2007Dammam Community College26 String Several built-in methods manipulate strings –LCase and UCase - converts case to lower and upper Imports Microsoft.VisualBasic Public Module StringMethods Sub Main() Dim StrName As String="TARIQ AZIZ" System.Console.print(LCASE(StrName)) End Sub End Module

27 Feb 23, 2007Dammam Community College27 Constants A variable that does not change Examples - tax rates, shipping fees, and values used in mathematical equations Declare a constant –Const keyword - Name is usually all uppercase –When you declare a constant, you need to assign the value to the constant –Const TAXRATE As Integer = 8 –Const Pi As Double = 3.14159 –Const greeting As String = “Good morning!”

28 Feb 23, 2007Dammam Community College28 Constants Exercise Write a small VB program that computes the area of a circle given its radius. Your program should include a constant declaration for the value of Pi.

29 Feb 23, 2007Dammam Community College29 Concatenation Process of joining one or more strings Concatenation operator (&) –Can also use (+) only with strings Join a literal string, or the result returned from an expression, or a variable that contains a string Dim str As String Str= "Tariq" + "Aziz" Str = Str & "Lecturer CA121"

30 Feb 23, 2007Dammam Community College30 Expressions Sequence of operands and operators that reduces to a single value. 3 3 + 2 3 + 2 * 4 What are the operands of the *? What about +? A primary expression consists of only a single operand which has no operator. It can be a name, a literal constant, or a parenthetical expression. A binary expression consists of one operator and two operands. Operators: Binary: +, -, *, /, \, Mod –Unary: +, -

31 Feb 23, 2007Dammam Community College31 Option Explicit and Option Strict Option Explicit On statement –Prevents you from using undeclared variables Implicit type conversion –Converts right-side value to datatype of left side –Promotion: data expanded; e.g., Integer to Decimal –Demotion: data truncated; e.g., Decimal to Integer Option Explicit On statement –Suppresses implicit conversions

32 Feb 23, 2007Dammam Community College32 Option Explicit and Option Strict (continued) Figure 3-19: Rules and examples of implicit type conversions

33 Feb 23, 2007Dammam Community College33 Option Explicit and Option Strict (continued) Figure 3-20: Option statements entered in the General Declarations section

34 Feb 23, 2007Dammam Community College34 + Operator Dim IntNumber As Integer Dim Strvar1 As String Dim Strvar2 As Integer IntNumber = 2 + 2 ' Returns 4. IntNumber = 4257.04 + 98112 ' Returns 102369. Option Strict On ' Initialize mixed variables. Strvar1 = "34" Strvar2 = 6 IntNumber = Strvar1 + Strvar2 ' Generates a compile-time error, disallows ' implicit conversion from "String" to "Double".

35 Feb 23, 2007Dammam Community College35 + Operator Option Strict Off StrVar1 = "34" IntVar2 = 6 IntNumber = StrVar1 + IntVar2 ' Returns 40 (addition) after the string in var1 ' is converted to a numeric value. ' Use of Option Strict Off for these operations ' is not recommended.

36 Feb 23, 2007Dammam Community College36 Assigning Data to an Existing Variable (continued) Figure 3-7: Literal type characters

37 Feb 23, 2007Dammam Community College37 Char Char variables are stored as unsigned 16-bit (2-byte) numbers Store a single text value as a number between 0 and 65,535 Represents a character in categories such as digit, letter, punctuation, and control characters Dim ChrVar As Char ' Cannot convert String to Char with Option Strict On ChrVar = "Z" ' Error. ChrVar = "Z"C ' Successfully assigns character Dim MyChar As Char MyChar = Chr(65) ' Returns "A". MyChar = Chr(97) ' Returns "a". MyChar = Chr(62) ' Returns ">". MyChar = Chr(37) ' Returns "%".

38 Feb 23, 2007Dammam Community College38 Numeric Integer number data types –Byte - stores an integer between 0 and 255 –Short - 16-bit number from -32,768 to 32,767 –Integer - 32-bit whole number –Long - 64-bit number Real number data types –Single - a single-precision floating point number –Double - larger numbers than the single data type –Decimal - up to 28 decimal places and often used to store currency data

39 Feb 23, 2007Dammam Community College39 Byte and Short Data Type Dim b As Byte b=20*12' 240 b=20*15' Error Dim s As Short s=2^15 -1 ' 32767

40 Feb 23, 2007Dammam Community College40 Integer and Long Data Type Dim i As Integer i=2^31 -1' 2147483647 i= 2147483647I ' 2147483647 i= 2147483647% ' 2147483647 i=-2^31' -2147483648 Dim l As Long l=9223372036854775807 l=9223372036854775807& l=-2^63&' -9223372036854775808

41 Feb 23, 2007Dammam Community College41 Single Data Type Single-precision numbers store an approximation of a real number The Single data type can be converted to the Double or Decimal data type Dim d1 As Single = 2 ^ 10.5 ' 1448.155 Dim d2 As Single = -2 ^ 10.5! ' -1448.155 Dim d3 As Single = -2 ^ 10.5F ' -1448.155 Appending the literal type character [ F or ! ] to a literal forces it to the Single data type

42 Feb 23, 2007Dammam Community College42 Double Data Type Double variables are stored as signed IEEE 64-bit (8-byte) double-precision floating- point numbers Dim d1 As Double = 2 ^ 10.5 ' 1448.15468787005 Dim d2 As Double = -2 ^ 10.5R ' -1448.15468787005 Dim d3 As Double = -2 ^ 10.5# ' -1448.15468787005 Appending the literal type character [ R or # ] to a literal forces it to the Single data type

43 Feb 23, 2007Dammam Community College43 Decimal Data Type ' No overflow. Dim d As Decimal = 9223372036854775807 ' overflow. Dim d As Decimal = 9223372036854775808 ' No overflow. Dim d As Decimal = 9223372036854775808D ' No overflow. Dim d As Decimal = 9223372036854775808@ This is because without a literal type character [D or @ ] the literal is taken as Long overflow

44 Feb 23, 2007Dammam Community College44 DateTime Dates between 01/01/0001 and 12/31/9999 Formats are mm/dd/yyyy and hh:mm:ss Enclosed within a pair of pound signs Dim MyBirthday As DateTime MyBirthday = #3/22/2002#

45 Feb 23, 2007Dammam Community College45 Boolean Data Type Two possible values: True or False In binary math: –1 represents true –0 represents false When numeric data types are converted to Boolean values, 0 becomes False and all other values become True. When Boolean values are converted to numeric types, False becomes 0 and True becomes -1. In Visual Studio.NET –True value is converted to -1 –False value is converted to 0 False True

46 Feb 23, 2007Dammam Community College46 Default Values The default value of decimal is equivalent to the literal 0D. The default value of an integral type (Byte, Short, Integer and Long) is equivalent to the literal 0 The default value of Boolean is False. The default value of the Date type is equivalent to the literal # 01/01/0001 12:00:00AM #. The default value of the Char type is ChrW(0). The default value of the String type is a null reference.

47 Feb 23, 2007Dammam Community College47 User-defined Data Types (UDT) The programmer can invent his own type using a structure statement. A structure can include multiple component variables (analogous to fields in a database record). Structure Student Dim Name As String Dim GPA As Double Dim id As Long End Structure... Dim s1 As Student s1.Name = “Saleh Al-Ghamdi”

48 Feb 23, 2007Dammam Community College48 User-defined Data Types Example Module M1 Structure Student Dim Name As String Dim GPA As Double Dim id As Long End Structure Sub main() Dim s1 As Student s1.Name = “Saleh Al-Ghamdi” s1.GPA=3.5 s1.id=200410213 system.console.writeline(s1.Name &","& s1.GPA &","& s1.ID) End Sub End module

49 Feb 23, 2007Dammam Community College49 User-defined Data Types Example Class M1 Structure Student Dim Name As String Dim GPA As Double Dim id As Long End Structure Shared Sub main() Dim s1 As Student s1.Name = “Saleh Al-Ghamdi” s1.GPA=3.5 s1.id=200410213 system.console.writeline(s1.Name &","& s1.GPA &","& s1.ID) End Sub End Class


Download ppt "Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz."

Similar presentations


Ads by Google