Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.

Similar presentations


Presentation on theme: "Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design."— Presentation transcript:

1 Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design

2 Visual Basic.NET Programming: From Problem Analysis to Program Design2 Exploring the Microsoft.NET Framework.NET Framework key parts: –Compilers for: VB.NET Other supported.NET languages –Common Language Runtime (CLR) –Framework Class Library (FCL)

3 Visual Basic.NET Programming: From Problem Analysis to Program Design3

4 4 The Microsoft.NET Compilers Compiler has two primary purposes: –Check source code for syntax errors –Translate it into executable form Compilers translate source code into code, which could be understood by the Common Language Runtime (CLR) and then executed by the computer

5 Visual Basic.NET Programming: From Problem Analysis to Program Design5 The Framework Class Library Consists of over 100 classes (a class defines what all objects of a group have in common) Files have suffix of.dll Namespaces –Organize classes –Can contain both classes and other namespaces –Compilers do not automatically search all namespaces for classes used by code Must use keyword Imports Tell compiler specific namespaces to access

6 Visual Basic.NET Programming: From Problem Analysis to Program Design6

7 7

8 8 Writing a Visual Basic.NET Module Definition Module definition –Begins with keyword Module –Ends with keyword End Module Statements contain: –Keywords –Identifiers

9 Visual Basic.NET Programming: From Problem Analysis to Program Design9 Writing a Visual Basic.NET Module Definition (continued) Identifier –Name assigned to things such as: Modules Procedures Variables

10 Visual Basic.NET Programming: From Problem Analysis to Program Design10 Writing a Visual Basic.NET Module Definition (continued) Identifier naming rules: –Can be up to 1023 characters long –Can include any: Letter Number Underscore character No spaces –Cannot begin with a number –Cannot be a keyword

11 Visual Basic.NET Programming: From Problem Analysis to Program Design11 Writing a Visual Basic.NET Module Definition (continued) Code not case sensitive Comment lines (e.g. ‘this is a comment) –Add explanations to code –Ignored by compiler Module header –Names module –Syntax: Module modulename

12 Visual Basic.NET Programming: From Problem Analysis to Program Design12 Writing a Visual Basic.NET Module Definition (continued) Procedure: –Contains statements that perform processing –Types: Sub Function –Begin with header Procedure Main invoked automatically

13 Visual Basic.NET Programming: From Problem Analysis to Program Design13 Writing a Visual Basic.NET Module Definition (continued) Argument –Information contained in parentheses when calling procedure (e.g. Console.Write(“argument”)) –Passed to procedure

14 Visual Basic.NET Programming: From Problem Analysis to Program Design14 Defining Visual Basic.NET Variables And Data Types Variable –Memory location that contains data –Characteristics: Name Data type Value

15 Visual Basic.NET Programming: From Problem Analysis to Program Design15 Understanding VB.NET Data Types Each variable has a data type Can be: –Primitive –Complex The computer allocates two bytes for each character and accommodates all characters of major international languages

16 Visual Basic.NET Programming: From Problem Analysis to Program Design16

17 Visual Basic.NET Programming: From Problem Analysis to Program Design17 Declaring and Populating Variables Declaration statements –Define variables Syntax: –Dim variablename As datatype Assignment operator –= –Assigns value on right side to variable named on left side

18 Visual Basic.NET Programming: From Problem Analysis to Program Design18 Examples Declaring Variables: Dim myInteger As Integer Dim myDouble As Double Dim myBoolean As Boolean Populating Variables myInteger = 1 myDouble = 2.5

19 Visual Basic.NET Programming: From Problem Analysis to Program Design19 Defining Constants Constant –Variable with a value that does not change –Contain values such as: Company name Tax identification number –Syntax: Const constantname As datatype –Must be initialized in the same statement that declares them

20 Visual Basic.NET Programming: From Problem Analysis to Program Design20 Defining Constants (continued) Naming convention: –Capitalize constant names –If name consists of more than one word Separate words with underscore character (_) –Example: TAX_ID

21 Visual Basic.NET Programming: From Problem Analysis to Program Design21 Module Module1 Sub Main() 'declare variables Dim exam_Score1, exam_Score2 As Integer Dim average As Double Dim studentCSIT108 As Boolean 'declare a constant Const PI As Double = 3.14 'populate variables exam_Score1 = 86 exam_Score2 = 96 'compute average score average = (exam_Score1 + exam_Score2) / 2 'display result Console.WriteLine("the average is: " & average) Console.WriteLine() End Sub End Module

22 Visual Basic.NET Programming: From Problem Analysis to Program Design22 Converting Data Types Numeric data types have different capacities: –Byte variable can hold maximum value of 255 –Integer variable has maximum value of 2.1 billion Implicit type conversion –Use assignment operator to assign contents of variable to a variable with different data type

23 Visual Basic.NET Programming: From Problem Analysis to Program Design23 Example 3-7: Implicit Type Conversion Dim myInteger As Integer = 1 Dim myDouble As Double = 2.5 myDouble = myInteger Assign Integer value to Double variable –Data type Double has greater capacity than Integer –No potential loss of data

24 Visual Basic.NET Programming: From Problem Analysis to Program Design24 Example 3-8: Loss of Precision Loss of precision –Computing error that can occur when decimal positions are dropped Dim myInteger As Integer = 1 Dim myDouble As Double = 2.5 myInteger = myDouble DO NOT DO THIS VB.NET will automatically round decimal values before truncating (cutting)

25 Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 3-8: Loss of Precision (continued) Option Strict –Prevent unintentional loss of precision when mixing data types in assignment statements –Compiler detects potential loss of precision Displays error message Explicit type conversion –Invoke Convert method to convert data types Do ex. 3-9, p. 96 Do ex. 3-11, p. 98 & Read p. 100

26 Visual Basic.NET Programming: From Problem Analysis to Program Design26 Module Module1 Sub Main() 'declare variables Dim numDouble as Double Dim numInt as Integer 'populate variables numDouble = 119.6 numInt = 119.6 ‘display result: the integer number is rounded up Console.WriteLine("integer: " & numInt) Console.WriteLine("double: " & numDouble) ‘display result: the integer number is rounded up numDouble = 11.634 numInt = NumDouble Console.WriteLine(“After assigning a decimal number/value to an integer variable, the integer variable contains " & numInt & “ ; the number assigned to it was “ & numDouble) End Sub End Module

27 Visual Basic.NET Programming: From Problem Analysis to Program Design27

28 Visual Basic.NET Programming: From Problem Analysis to Program Design28 Converting Data Types (continued) Option Explicit –Must define variable before using it in a statement –Otherwise Compiler generates error message –Generally set On

29 Visual Basic.NET Programming: From Problem Analysis to Program Design29 Using Reference Variables Uses class name as data type For example: –String Variable refers to or points to instance of class –Does not actually contain data –Contains memory address of instance of class that contains data

30 Visual Basic.NET Programming: From Problem Analysis to Program Design30

31 Visual Basic.NET Programming: From Problem Analysis to Program Design31 Using the Arithmetic Operators Arithmetic operators –For multiplication, division, addition, and subtraction –*, /, +, - Evaluated in predetermined order called precedence –Standard algebraic rules of precedence apply (p. 101) Other operators: –Exponentiation –Integer division –Remainder computation

32 Visual Basic.NET Programming: From Problem Analysis to Program Design32

33 Visual Basic.NET Programming: From Problem Analysis to Program Design33 Example 3-15: Integer Division (\) Dim firstInt As Integer Dim secondInt As Integer Dim integerResult As Integer = 0 firstInt = 11 secondInt = 2 integerResult = firstInt \ secondInt Console.WriteLine(“integerResult = firstInt \ secondInt: “ & integerResult) Result: 5, not 5.5 Do ex. 3-13 & 3-14, p.102

34 Visual Basic.NET Programming: From Problem Analysis to Program Design34 Using the Arithmetic Operators (continued) Assignment operators: –Formed by combining arithmetic operator with assignment operator –Example: i += 1 is the same as i = i + 1 See table 3-6, p. 105

35 Visual Basic.NET Programming: From Problem Analysis to Program Design35 Invoking Methods in the Math Class System namespace includes Math class –Contains methods to accomplish Exponentiation Rounding Trigonometric calculations See table 3-6, p. 105 To invoke (use) a method from the class library: –Use word “Imports” is necesary –Syntax: ClassName.MethodName(argument) –Math.Pow(firstInt, secondInt)

36 Visual Basic.NET Programming: From Problem Analysis to Program Design36 Invoking Methods in the Math Class (continued) Math class constants: –PI and E (exponent) –To access: Math.PI Do ex. 3-18, 3-19, 3-20, p. 106

37 Visual Basic.NET Programming: From Problem Analysis to Program Design37 Reading Input From the Keyboard Use Console class (part of the System namespace) –ReadLine method: Console.ReadLine() Read one or more characters from keyboard Convert any numeric data to desired data type Prompt –Message displayed to user asking for input Do ex. 3-21, p. 108

38 Visual Basic.NET Programming: From Problem Analysis to Program Design38 Programming Example: Temperature Converter Input –Fahrenheit temperature including decimal positions Output –Celsius temperature that corresponds to Fahrenheit temperature that was input Results rounded to one decimal position

39 Visual Basic.NET Programming: From Problem Analysis to Program Design39 Programming Example: Temperature Converter (continued) Main Algorithm –Declare variables –Prompt user for input –Invoke Console.ReadLine to input value from keyboard –Invoke Convert.ToDouble to convert data type String to Double

40 Visual Basic.NET Programming: From Problem Analysis to Program Design40 Programming Example: Temperature Converter (continued) Main Algorithm (continued) –Compute Celsius temperature –Invoke Math.Round to round computed value to one decimal position –Invoke Console.WriteLine to display rounded Celsius temperature

41 Visual Basic.NET Programming: From Problem Analysis to Program Design41 Summary Framework consists of: –Compilers –Common Language Runtime (CLR) –Framework Class Library (FCL) VB.NET statements consist of –Keywords –Identifiers

42 Visual Basic.NET Programming: From Problem Analysis to Program Design42 Summary (continued) Module –Main module executed automatically Procedures: –Subs –Functions Variable –Memory location that contains data

43 Visual Basic.NET Programming: From Problem Analysis to Program Design43 Summary (continued) Constant –Variable with value that does not change Reference variable –Uses class name as data type Operators: –Mathematic –Assignment For in-class practice: p. 113, Ex. 2, 3, 6, 7, 8, 9, 11, 14; p. 114, PEx. 2, 5; Program: AddTwoNumbers Homework: p. 114, PEx. 3, 4; Due: 2/1/08


Download ppt "Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design."

Similar presentations


Ads by Google