140 likes | 224 Vues
Research Topics in Computational Science. Agenda. Commenting Our Code Variable Review Button Program – Add Textbox Getting text from textbox Converting text from textbox. Commenting. Makes code easier to read Helps you to remember Single & Multi-line comments // <- single line comment
E N D
Agenda • Commenting Our Code • Variable Review • Button Program – Add Textbox • Getting text from textbox • Converting text from textbox
Commenting • Makes code easier to read • Helps you to remember • Single & Multi-line comments • // <- single line comment • /* multiline comment */ • Be sure to comment your code!
Declaring Variables • Many ways to do this: int age; // Most Common int age = 23; // Most Common <something> int age; public int age; // We will learn more about private int age; // these examples later
Variable Assignment • x = 1+2+3; • The right hand side will be computed, then that value is assigned to x
Good Variable Names • CamelCase • Capitalize the first letter of each word • Long names are good • Debate: To capitalize the first letter? • ALWAYS when it is a class • not always for variables
Math Operations • Core 4: +, -, *, / • Special: % (remainder after division) • What is -1%7? • More math operations found in System.Math
Logic • We can use a logic variable • Or Compare 2 variables • Compare Logic: • A and B, A or B, A xor B, A equal B • A && B, A || B, A ^ B, A == B • Compare Numbers: • x < y, x>y, x<=y, x>=y, x==y, x!=y • Comparing Strings is Trickier
Let’s Make a Program • Add a button • default name: button1 • Add a textbox • default name: textBox1
Convert Class • In our program we will use the class: Convert to convert from a string to a number. • This can convert between many data types and is useful to know about. • Usage (for Int32): x = Convert.ToInt32(inputString);
Try-Catch • When don’t want our program to crash from a runtime error we can use Try-Catch • Quickly create by typing try-”tab”-”tab” try { } catch (Exception ex) { mbox.Show(ex.Message()); }
If, Else if, Else double a, b, c; … if ( a>b) { // do something } else if ( a == b ) { // do something else } else { // if both above fail, do this }
Switch int age; … switch (age) { case 20: //do this if age == 20 break; case 25: // do this if age == 25 break; default: // do this if all the above cases fail break;