Chapter 3: Introducing the Microsoft .NET Framework and Visual Basic .NET Visual Basic .NET Programming: From Problem Analysis to Program Design Exploring the Microsoft .NET Framework • .NET Framework key parts: – Compilers for: • VB .NET • Other supported .NET languages – Common Language Runtime (CLR) – translates files from the compilers into executable files – Framework Class Library (FCL) – contains hundreds of prewritten classes Visual Basic .NET Programming: From Problem Analysis to Program Design 2 Visual Basic .NET Programming: From Problem Analysis to Program Design 3 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 4 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 5 Visual Basic .NET Programming: From Problem Analysis to Program Design 6 Visual Basic .NET Programming: From Problem Analysis to Program Design 7 Writing a Visual Basic .NET Module Definition • Module definition – Begins with keyword Module – Ends with keyword End Module • Statements contain: – Keywords: Module, Sub, End Sub, etc. – Identifiers: Main, firstName, number1, etc. Visual Basic .NET Programming: From Problem Analysis to Program Design 8 Writing a Visual Basic .NET Module Definition (continued) • Identifier – Name assigned to things such as: • Modules • Procedures • Variables Visual Basic .NET Programming: From Problem Analysis to Program Design 9 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 10 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 11 Writing a Visual Basic .NET Module Definition (continued) • Procedure: – Contains statements that perform processing – Types: • Sub – does not return a value • Function – returns a value – Begin with header • Procedure Main invoked automatically Visual Basic .NET Programming: From Problem Analysis to Program Design 12 Writing a Visual Basic .NET Module Definition (continued) • Argument – Information contained in parentheses when calling procedure (e.g. Console.Write(“argument”)) – Passed to procedure Visual Basic .NET Programming: From Problem Analysis to Program Design 13 Defining Visual Basic .NET Variables And Data Types • Variable – Memory location that contains data – Characteristics: • Name • Data type • Value Visual Basic .NET Programming: From Problem Analysis to Program Design 14 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 15 Visual Basic .NET Programming: From Problem Analysis to Program Design 16 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 17 Examples • Declaring Variables: Dim myInteger As Integer Dim myDouble As Double Dim myBoolean As Boolean • Populating Variables myInteger = 1 myDouble = 2.5 Visual Basic .NET Programming: From Problem Analysis to Program Design 18 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 it Visual Basic .NET Programming: From Problem Analysis to Program Design 19 Defining Constants (continued) • Naming convention: – Capitalize constant names – If name consists of more than one word • Separate words with underscore character (_) – Example: • TAX_ID Visual Basic .NET Programming: From Problem Analysis to Program Design 20 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 21 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 22 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 23 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) Visual Basic .NET Programming: From Problem Analysis to Program Design 24 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 25 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 26 Visual Basic .NET Programming: From Problem Analysis to Program Design 27 Converting Data Types (continued) • Option Explicit – Must define variable before using it in a statement – Otherwise • Compiler generates error message – Generally set On Visual Basic .NET Programming: From Problem Analysis to Program Design 28 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 29 Visual Basic .NET Programming: From Problem Analysis to Program Design 30 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 31 Visual Basic .NET Programming: From Problem Analysis to Program Design 32 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 33 Using the Arithmetic Operators (continued) • Assignment operators: – Formed by combining 1 of the arithmetic operators with the assignment operator – Example: • i += 1 is the same as i = i + 1 See table 3-6, p. 105 Visual Basic .NET Programming: From Problem Analysis to Program Design 34 Invoking Methods in the Math Class • System namespace includes Math class – Contains methods to accomplish • Exponentiation • Rounding • Trigonometric calculations See table 3-7, p. 105 • To invoke (use) a method from the class library: – Use word “Imports” is necessary – Syntax: ClassName.MethodName(argument) • Math.Pow(firstInt, secondInt) Visual Basic .NET Programming: From Problem Analysis to Program Design 35 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 36 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 37 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 38 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 39 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 40 Summary • Framework consists of: – Compilers – Common Language Runtime (CLR) – Framework Class Library (FCL) • VB .NET statements consist of – Keywords – Identifiers Visual Basic .NET Programming: From Problem Analysis to Program Design 41 Summary (continued) • Modules • Procedures: – Subs – Functions – Procedure Main invoked automatically • Variable: – Memory location that contains data Visual Basic .NET Programming: From Problem Analysis to Program Design 42 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, 3, 4, 5; Program: AddTwoNumbers Visual Basic .NET Programming: From Problem Analysis to Program Design 43
© Copyright 2025 Paperzz