review of fundamentals
Quick Review
Variables: types ranges assignment scope lifetime casts conversions Control structures: relational & logical operators loops (while, do/while, for) branches(if, else if, else) Math: arithmetic operators accumulators class(.pow, .sqrt) Files: open read reset file pointer Methods: parameters return
Details
REVIEW for 244 ===================================================================== -Algorithms pseudocode IPO -Computer and Programming Environment HW CPU, RAM, SS, I , O SW .Net Framework Visual Studio IDE big 5 components .Net Class library Compiler CLR, IL, (JIT) "The CLR JITs the IL generated by the .Net compiler after creation of the C# program in the IDE." -Windows programs Classes and objects Properties Methods Events GUI Control objects Tabstop, Tabindex, Access (hot) Keys & AcceptButton, CancelButton -Event-driven code ("event handler") click, text_changed, hover, tick event wiring connects code to event -Controls Form Button TextBox ListBox Label PictureBox Timer RadioButton Dialog TabControl CheckBox GroupBox - Classes & Objects instantiation Class methods vs. instance methods Math vs Random -Variables Data types int, decimal, double, string, bool nullable (?) int? x = null; Naming meaningful, camel-case Declare, Initialize int x; decimal d = 789.88m; Class level variables (field) Scope { } Lifetime Strings NO conversion needed to input or output string s = txtIn.Text; Conversion of Strings to Numbers explicit int.Parse( ), double.Parse( ), decimal.Parse( ) int.TryParse(), etc... ToString() Conversion of Numbers to Numbers implicit int to decimal or double ok, narrow to wider double to int, decimal to int need cast, wide to narrower decimal d = 99.99m; int i = (int)d; // narrows and loses the .9 double to decimal, decimal to double both conversions need cast double x; decimal y = 3.5m; x = (double)y; -Assignment = val <-- expression variable: x = 99; property: txtOut.Text = "Hello"; - Accumulation counters initialize (usually to 0) +=, *=, -=, /= ++, -- prefix, postfix txtOut += " more text"; TextBox.AppendText - adds to what is already in textBox ListBox.Items.Add - adds to what is already in ListBox -Arithmetic + - * / % PEMDAS x = y + z * 3; Math class - no instance Math.Sqrt -Input TextBox.Text Conversion of Numbers int.Parse( ), double.Parse( ), decimal.Parse( ) int x = int.Parse(txtIn.tText); string s = txtIn.tText; -Strings strings represented in "double quotes" comparison if (s == "hello") input and output of numbers requires CONVERSION! .Parse() .ToString() join (+) append (+=) -Handling Exceptions user input int.TryParse() generally try { } catch (Exception ex) { // can report the exception } -Output Must be a string or converted to a string Text Property of any control = TextBox.Text = Label.Text = ListBox.Items.Add( ) MessageBox.Show( ) ToString() "C", "P", "N" -Conditions (comparison) - always true or false relational operators < > <= >= != == logical operators && || ! compound conditions -Selection if (condition) if (condition) else if (condition) else if (condition) else if (condition) else -Loops do { code } while(condition); while(condition) { code } for(int i=0; i<10; i+++) { code } -Random Numbers (class, object) Random rnd = new Random(); // create a random object from the Random class rnd.Next(0,101); - constructor -Files as input using System.IO; StreamReader reader = new StreamReader( "File.txt" ); -File Dialogs openFileDaialog.ShowDialog(); if ( openFileDialog.ShowDialog() == DialogResult.OK ) user choice stored in openFileDialog.FileName -Readability refactor explain WHY spacing indentation -Errors Syntax Logic Run-time exceptions -Methods, Parameters private void DoIt() { MessageBox.Show("Hello"); } private void DoIt(string herName) { MessageBox.Show("Hello " + herName); } -------------------------------------------- private int Twice(int num) { return num * 2; } OR private int Twice(int num) { int result; result = num * 2; return result; }
C# & .Net
Create project via Visual Studio 'IDE' Visual Studio compiler translates C# into "IL" or ""MSIL" or "CIL" CLR interprets IL into binary happens when code is executed (JIT) called "managed code" (manged by CLR) binary code (machine code) is the ONLY 'language' that the hardware actually 'understands'. "Native code" involves straight compile from source to binary We will do this later using C++.


Code we will create: console windows library (dll) **
void Main() { for (int i =0; i< 1000;i++) { if(i%11 == 0) Console.WriteLine(i); } } MSIL [cpu independent - converted to NATIVE CODE (binary) by the Common Language Runtime (CLR)] IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: br.s IL_0014 IL_0004: ldloc.0 IL_0005: ldc.i4.s 0B IL_0007: rem IL_0008: brtrue.s IL_0010 IL_000A: ldloc.0 IL_000B: call System.Console.WriteLine IL_0010: ldloc.0 IL_0011: ldc.i4.1 IL_0012: add IL_0013: stloc.0 IL_0014: ldloc.0 IL_0015: ldc.i4 E8 03 00 00 IL_001A: blt.s IL_0004
Development Process
analysis => requirements contract design => approach algorithms coding => works correct under all conditions fault tolerant maintainable testing => boundary conditions unit testing integration & testing delivery => maintenance (50-80% of lifetime cost) documentation and metadata refactor - make understandable - does not change functionality