using System.ComponentModel.Design; using System.Reflection.Metadata.Ecma335; using System.Security.Cryptography; using System.Text.RegularExpressions; Console.WriteLine("Section 2 Examples"); Console.WriteLine("=================="); Console.WriteLine("Chapter 4"); // Built in value types page 93 // Variable Declaration page 95 // 1. Did I declare it? // 2. Did I give it value? value or operational value // 3. Did I return it? itself or something else // Method 1. 2 step process // declare the variable string firstname; string lastname; // set the value of the variable in memory firstname = "Bob"; lastname = "Smith"; // page 110 join strings + // Method 2. one statement that declares and sets the value string fullname = firstname + " " + lastname; //Bob Smith // return Console.WriteLine(fullname); // Bob Smith // page 113 literal vs logical code string quoted = "Bob said \"Type code for readability\"."; Console.WriteLine(quoted); // Page 119 Converting and Parsing // Parse "parses" for just that moment in code // Convert "converts" the actual object type decimal productPrice = 25.52m; // decorate decimal with 'm' // en/US locale setting string productDesc = "Widget 1 costs " + productPrice.ToString(); Console.WriteLine(productDesc); // Convert Class // All inputs in OOP by default are strings // input options > form webpage, readline terminal, json, csv, txt, databases, etc... string i_zipCode = "32765"; // use the zipCode as an int // create a NEW variabale int and convert the string int zipCode = Convert.ToInt32(i_zipCode); // Page 123 "scope" or "context" // defines a block of codes operations or variables // scope of a class // scope of a function // scope of a method void myCalc() { int x = 7; int y = 3; int z = x + y; Console.WriteLine(z); // does exist in the scope of the method // do something to access outside of this scope } // Console.WriteLine(z); // does not exist in the scope of class Console.WriteLine("Chapter 5 \n\n"); // add 2 line breaks to the output Console.WriteLine(); // also add an empty line to the output // page 139 relational operators == != >= <= > < ... // page 140 logical operators & | ! test variable has to be defined on BOTH sides of the logical operator // page 143 if-else statement keywords if, else if, else // return type boolean // Not a requirment to define the FALSE return "aka, you do not need to use the else keyword" // Ask the user for their age "input" type default string Console.WriteLine("Enter your age: "); // type string string i_age = Console.ReadLine(); // as a string // page 148 regualar expressions // test the pattern of a string // regex101.com testing // regexlib.com examples // regular expression test for ^\d{7}$ string agePattern = "^[0-9][0-9]$"; if (Regex.IsMatch(i_age, agePattern)) { Console.WriteLine("You entered a valid age"); } else { Console.WriteLine("You entered an invalid age, must be a number between 0-99"); // loop back to askign the user for a new input } int age = Convert.ToInt32(i_age); // converted to int // int age = 43; // declare a variable named age as type integer assigned value of 16 string state = "FL"; // drive 16 "add another condition" if // vote 18 else if // drink 21 else if // retire 65 else if // else < 16 if (age >= 18 && age < 21) { Console.WriteLine("You can vote but not drink or retire"); } else if (age >= 21 && age < 65) { Console.WriteLine("You can vote and drink but not retire"); } else if (age > 65) { Console.WriteLine("You can vote, drink and retire"); } else { Console.WriteLine("You are not 18 or over"); } // switch case statement page 144 // test variable is age on line 77 currenly assigned as 43 years old // keywords switch case break goto default switch (age) { case >= 65: Console.WriteLine("You can drive, vote, drink and retire."); break; case >= 21: Console.WriteLine("You can drive, vote and drink."); break; case >= 18: Console.WriteLine("You can drive and vote"); break; case >= 16: Console.WriteLine("You can drive"); break; default: Console.WriteLine("You are too young to drive, vote, drink ro retire."); break; } // switch Console.WriteLine("Chapter 5 \n\n"); // pages 156 & 158 // for (control, condition, return) { operation } for (int i = 1; i < 10; i++) { Console.WriteLine(i); // 1<10; 1; i=2 // 2<10; 2; i=3 // 9<10; 9; i=10 // 10<10; break } // return to the next scope // scope of this context // for loop using a ListSet, loop thru firstnames string first_name = "Bob"; // type: string name: first_name // type: string is "data" type // types: data,Class,reference,Service,Custom,delegate List first_names = new List { "Bob", "Joe", "Mary" }; // length of List 3 // positional index 0:2 // Joe first_names[1] Console.WriteLine("List of Contact First Names"); Console.WriteLine("==========================="); for (int i = 0; i < first_names.Count; i++) { int id = i + 1; Console.WriteLine(id + ". " + first_names[i]); // increment of i } Console.WriteLine("Record id to update: "); string i_id = Console.ReadLine(); // "1" int r_id = Convert.ToInt32(i_id); // 1 int u_i = r_id - 1; // 0 Console.WriteLine("What do you want the new first name to be"); string u_firstname = Console.ReadLine(); // read user input first_names[u_i] = u_firstname; // first_names[0] = "Robert"; Console.WriteLine("New List of First Names"); Console.WriteLine("==========================="); for (int i = 0; i < first_names.Count; i++) { int id = i + 1; Console.WriteLine(id + ". " + first_names[i]); // increment of i } // for loop Console.WriteLine("Chapter 6"); Console.WriteLine("========="); // page 170 // accessType returnType methodName() { ops; } page 171 //// accessType -> nothing(scope) public private "protected" //// returnType -> void any boolean "something else" //// () empty params (x,y) parameterized (x,y="0") query string params //// methodName best practice to be camelCase or underscore_case // declare or "define" the method void myCalculator(int x, int y) { // 2 values are passed outside the method // calculate z int z = x + y; // return of z last step // test z for condition // if ( z < 100 ) {true} Console.WriteLine(z); // Last step defines the return } // myCalculator // somewhere else in life.... call a function // if you can not see the function in the drop down, you can not access it myCalculator(7, 3); // call the function and passing the arguments to the parameters "call, pass, args, params"