1 / 48

MIS 3200 – Unit 2.1

MIS 3200 – Unit 2.1. Outline Variables Arithmetic Formatting Conversion . Data Storage. Computers typically store things in two major places Internal memory – temporary storage e.g. a notebook computer with 4GB memory External storage – long term storage

sydney
Download Presentation

MIS 3200 – Unit 2.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIS 3200 – Unit 2.1 • Outline • Variables • Arithmetic • Formatting • Conversion

  2. Data Storage • Computers typically store things in two major places • Internal memory – temporary storage • e.g. a notebook computer with 4GB memory • External storage – long term storage • Typically on hard disks, solid-state devices, “flash” drives”, CDs, DVDs, etc.

  3. Data Types • Data comes in different “types” • Numbers • Whole numbers • 1, 324567, -25, etc. • Numbers with a decimal place • 25.75, 0.05, 1234567.0, etc • Text • “Copeland Hall 209”, “Joe Bobcat”, “Hello XYZ Shoppers!” • Logical data • true, false

  4. Data Types Commonly used data types are highlighted

  5. Usage Rules • Prefer data types that use less space • decimal versus double • Decimal and decimal types use the suffix “m” • Strings always use double quotes • Case sensitivity matters • Upper case data types are objects • Lower case data types are just variables

  6. Assigning Variables • Variables are assigned a value with the =operator, either in the declaration statement, • or later in the method after the variable is declared • Variables may be assigned the value stored in another variable A technical note: The = operator is called the assignment operator, it does not mean that two things are equal. For example, intCreditHours = 16 says that the variable intCreditHours is assigned a value of 16, it does not say that it is equal to 16. The difference will be important when we learn how to make decisions.

  7. Storing data into variable names • Each value must be stored in a container with a name. • Decide on an appropriate data type • string for usernames • int for number of work days in the week • Then decide an appropriate name • strUserName • intNumWeekDays • Then write the C# sentence • string strUserName = txtUserName.Text; • intintNumWorkDays = 6;

  8. Usage Examples Int32 intMonths = 12; intintNumStudents = 25548612; stringstrUserName = “Matta”; StringstrPassword = “Vic”; decimal decAnnualSavings= 12,456.00m; decimal decSavingsPerMonth = decAnnualSavings/intMonths;

  9. Declaring Variables • Variables live inside methods – they are created when the method starts and are destroyed when the method ends. • Variables are generally declared at the top of a method, just after the opening brace

  10. Exercise: List Data types for … • The US debt of $16,738,649,841,392.70 • US population of 316,586,633 • Number of students in the class 36 • A student: Gabriela • This class is hard (false)

  11. Assigning Variable - 2 • Variables may also be assigned a calculated value (more on calculations on the next slide) The resulting value is stored in the variable on the left side of the = (the variable has to be of the same type as the data being stored). This is a decimal value multiplied (the * means multiply) by an integer value. The result is a decimal value. Assignment statements ALWAYS take the value on the right side and store it in the variable on the left side

  12. Math operations • C# uses the set of arithmetic operators common to most computer languages

  13. Order of operations • If you have several arithmetic operators in the same expression, e.g. 2+3*4/5-6, • * and / take place before + and – • If both * and / are present they are processed from left to right • If both + and – are present they also are processed from left to right • Parenthesis can be used to force one operation before another

  14. Conversions • Data stored in a variable must be of the same type as the variable • The Text property value of a TextBox or Label is always a string and cannot be stored directly in a numeric variable as shown in this message generated by VS Express

  15. Convert • The previous error message said it couldn’t implicitly converttype string (the value of the TextBox) to int (the data type of the variable) • C# provides a Convert object to help us with this

  16. The Convert object • The Convert object has a method for just about any kind of conversion you might want to do Some of Convert’s methods that are displayed when you enter Convert. In VS Express

  17. Converting TextBox data • methods always have () • To convert data in a TextBox, list the TextBox and the Text property inside the Convert methods () Note: The Convert operation will fail if there is any improper data in the TextBox. For example, Convert.ToInt32 will fail if the TextBox is empty or has any characters that aren’t allowed in integers. We will see how to prevent this in a later unit.

  18. Example: Writing logic to test whether 4 > 2 • Are both data types the same? • Yes • What are their data types? • int • Run the conditional statement: • If (4>2) // then do this; • Are both data types the same? • No (assume “4” with 4) • What are their data types? • “4” is a String data type • 2 is an int data type • Convert the data first • Convert.ToInt32(“4”) • Then run the conditional • If (Convert.ToInt32(“4”)>2) // then do this;

  19. Converting TextBoxdata #2 Let’s say that you want to test a condition, is 2 > 4 (the answer of course is no, 2 isnot greater than 4).If you tried to test another condition, is the word “2” > 4, the answer is "Idon't know. How do I compare the word (string) “2” to the number 4?"  It islike asking if “pie” is greater than 3.14.  It is not a logicalcomparison since you are asking if a word is greater than a number.Instead, if we first convert the word "2" into a numberwe could say something like this, is Convert.ToInt32("2") > 4, Then the comparison to evaluate will be: is 2 > 4, and now we get the Answer no, 2 is not greater than 4 (which is what we were expecting).When you use the Text property of a Label or a TextBox, the value isalways treated like a word (string).  It MUST be converted to a numerical datatype if you want to make a numerical comparison or perform a calculation.

  20. And, on the flip side • Whenever you assign data to a Label or TextBox that data must be string data • meaning it must always be in double-quotes “” e.g. lblWelcome.Text=“Welcome!”; • All objects have a method called ToString(). For numeric variables the ToString() method converts the numeric value of the variable to a stringlblOutput.Text = decSalesTaxRate.ToString();

  21. Formatting output • ToString() does a generic conversion that may not be what you want • To get around this problem (when working with numeric data types) we can add a format code inside the ()

  22. Format codes • A list of format codes and examples of how they work can be found at • http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx • For example: Common codes: “C” or “c” - currency “F” or “f” – fixed point “P” or “p” – percentage See the link above for specific examples

  23. Unit 2 – L1 • Time to try it out • We will develop a simple 4 function calculator • Be sure that your ASPPub is on your desktop • Open Visual Web Developer • Open the ASPPub web site

  24. L1 #2 • Right-click on Unit2 under MIS3200 and Add New Item • Make sure Visual C# is selected • Select Web Form • Name the form yourLastNameU2L1.aspx • Make sure the two boxes are checked • Click Add • Select Site.master from ASPPub • Click OK (see next slide)

  25. L1 #3 1 2 3 7 5 4 8 6

  26. L1 #4 • Switch to Design view • Click in MainContent and type Unit 2 L1 – Simple Calculator and then press Enter • Select all the text • Use the BlockFormat dropdown list to convert the text to an Heading 2

  27. L1 #5 • Click on the column to the right of Style in the Properties window • Click to open the Style editor window • Change the Background background-color to the same green you used on your master page

  28. L1 #6 • Change the Font color to White • Click OK

  29. L1 #7 • Insert a Table with 4 rows and 2 columns in the paragraph after your heading. Set the table width to 50%

  30. L1 #8 • Type Number 1 in row 1, col 1 • Type Number 2 in row 2, col 1 • Add a TextBox to row 1, col 2, and change the (ID) to txtNumber1 • Add a TextBox to row 2, col 2 and change the (ID) to txtNumber2

  31. L1 #9 • Select the entire third row of the table by clicking in the left cell and dragging into the right cell – you should see both cells highlighted • From the Table menu, select Modify and Merge Cells (this createsa single cell with the ColSpan property with set to 2)

  32. L1 #10 • With the merged cell selected • Open the Style editor (see slide 27 for example) • Select Block, text-align to center • Add a button to the cell • Change the (ID) to btnAdd • Change the Text to + • Add three additional buttons and change their properties

  33. L1 #11 • Notice that the buttons have slightly different sizes. It is a good design principle to keep related buttons the same size. • Select each button and change the Width property to 30px (px means pixel)

  34. L1 #12 • Merge the two cells in the bottom row and set the text-align to center • Add a label to the last row • Change the (ID) to lblOutput • Clear the Text property • Change Visible to false

  35. L1 #13 • Double click the + button to create the click event method • Add appropriate method level comments • After the comments create three decimal variables called decNum1, decNum2 and decSum and a string variable called strOutput

  36. L1 #14 • Convert the Text in txtNumber1 to a decimal value and store in decNum1 • Repeat the process for the second number • Then, add the two numbers and store the result in decSum • Create a output message and store it in strOutput(see next slide)

  37. L1 #15 Convert the decimal numbers to strings using the F2 format which displays two decimal digits A: Concatenate (stick together) the formatted value stored in decNum1 with the string inside the quotes – a space followed by a + followed by a space C: Concatenate the results of step B with the string inside the quotes – a space followed by a = followed by a space B: Concatenate the formatted version of decNum2 to the string created in step A D: Concatenate the formatted version of decSum to the string created in step C

  38. L1 #16 • Assign the formatted output to lblOutput • Change the visibility of lblOutput to true • Click in the Page_Load method • Set focusto txtNumber1 • Add appropriate page level comments

  39. L1 #17 • Save everything • Open the U2L1 aspx file in Design mode • Click Run • Enter two numbers such as 55 and 27 • Click the + button • You should see …

  40. L1 #18 • If everything is working • Open your MIS3200 home page in the MIS3200 folder • Switch to Design view • Position the cursor after your Unit 1 assignments and press Enter

  41. L1 #19 • Drag a HyperLink control into the new paragraph • Change the (ID) to hlUnit2L1 • Change the Text to Unit 2 L1 • Change the NavigateUrl to the page you just created • Save the page • Open Default.aspx in ASPPub • Run the page and be sure you can navigate to Unit 2 L1 • Submit the URL corresponding to this Default pagei.e. http://aspnet.cob.ohio.edu/yourOhioID/asppub/ • Also submit the direct URL corresponding to the page you just made: http://aspnet.cob.ohio.edu/yourOhioID/asppub/MIS3200/Unit2/yourLastNameU2L1.aspx • No screenshots are needed

  42. Unit 2 L2 • For this exercise we will finish the calculator begun in L1 • Open ASPPub in VS Express as before • Open the Unit2 folder and right-click on your L1 aspx page and pick Copy (see next slide) • Right-click on the Unit2 folder and pick Paste • Right-click on the copied file and rename it to yourLastNameU2L2.aspx

  43. Copy L1 and rename L2 MIS3200 MIS3200 MIS3200

  44. L2 #1 • Open the newly copied L2 aspx file in Design view • Change the heading to say Unit 2 L2 • We have one clean-up detail leftover from L1 – we should clear the contents of the textboxes after the button is clicked • Click on the + button and go to the bottom of the method • Clear our the textboxes by assigning an empty string “” to their Text property

  45. L2 #2 • Return to the U2L2 aspx file in design view • Double-click the – button to create the click event method for Subtraction • Add comments similar to what you had for Addition • Using btnAdd_Click as a pattern • write code to subtract number2 from number1 • display the results (see slide 38, step 39 for an example) • make any necessary changes to convert addition to subtraction and use appropriate variable names

  46. L2 #3 • Your code should look something like this • And produce output like this

  47. L2 #4 • Repeat the process for the * and the / buttons, including the comments • Be sure to test all functions • Link the finished L2 file to you MIS220 home page • The (ID) should be hlUnit2L2 • The Text should be Unit 2 L2 • The NavigateUrl should link to you new L2 • Put your ASPPub back on ASPNET and submit • your MIS Portfolio URL to the drop box • Your L2 page URL to the dropbox

  48. Think About It! • What are variables? • What is the purpose of different data types? • Why do we need to convert? • What arithmetic operators are commonly used in C#? • What is the purpose of the following: • Convert.ToDecimal() • .ToString()

More Related