mis 3200 – unit 2.1

Download MIS 3200  – Unit 2.1

Post on 14-Feb-2016

37 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

MIS 3200 Unit 2.1Outline VariablesArithmeticFormattingConversion 1Data StorageComputers typically store things in two major placesInternal memory temporary storagee.g. a notebook computer with 4GB memoryExternal storage long term storageTypically on hard disks, solid-state devices, flash drives, CDs, DVDs, etc.Data TypesData comes in different typesNumbersWhole numbers1, 324567, -25, etc.Numbers with a decimal place25.75, 0.05, 1234567.0, etcTextCopeland Hall 209, Joe Bobcat, Hello XYZ Shoppers!Logical datatrue, false

Data TypesData TypeSignedBytesRangeShortYes2-32,768 to 32,767intYes4-2,147,483,648 to 2,147,483,647Int32Yes4-2,147,483,648 to 2,147,483,647longYes8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Int64Yes8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807doubleYes8Approx 5.0 x 10-324to 1.7 x 10308with 15 or 16 sig figuresdecimalYes12Approx 10-28to 7.9 x 1028with 28 or 29 significant figurescharN/A2Any single Unicode character (16 bit)boolN/A1 / 2true or falsestringN/A..TextCommonly used data types are highlightedPrefer data types that use less spacedecimal versus doubleDecimal and decimal types use the suffix mStrings always use double quotesCase sensitivity mattersUpper case data types are objectsLower case data types are just variablesUsage RulesAssigning VariablesVariables 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.Storing data into variable namesEach value must be stored in a container with a name. Decide on an appropriate data typestring for usernames int for number of work days in the weekThen decide an appropriate namestrUserName intNumWeekDaysThen write the C# sentencestring strUserName = txtUserName.Text; int intNumWorkDays = 6;Usage ExamplesInt32 intMonths = 12; int intNumStudents = 25548612;string strUserName = Matta;String strPassword = Vic;decimal decAnnualSavings = 12,456.00m;decimal decSavingsPerMonth = decAnnualSavings/intMonths;Declaring VariablesVariables 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

Exercise: List Data types for The US debt of $16,738,649,841,392.70US population of 316,586,633Number of students in the class 36A student: GabrielaThis class is hard (false)Assigning Variable - 2Variables may also be assigned a calculated value (more on calculations on the next slide)

This is a decimal value multiplied (the * means multiply) by an integer value. The result is a decimal value.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).Assignment statements ALWAYS take the value on the right side and store it in the variable on the left sideMath operationsC# uses the set of arithmetic operators common to most computer languagesArithmetic operationC# operatorAddition+Subtraction-Multiplication*Division/Modulo (remainder)%Order of operationsIf 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 rightIf both + and are present they also are processed from left to rightParenthesis can be used to force one operation before anotherConversionsData stored in a variable must be of the same type as the variableThe 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

ConvertThe previous error message said it couldnt implicitly convert type string (the value of the TextBox) to int (the data type of the variable)C# provides a Convert object to help us with thisThe Convert objectThe Convert object has a method for just about any kind of conversion you might want to do

Some of Converts methods that are displayed when you enter Convert. In VS ExpressConverting TextBox datamethods 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 arent allowed in integers. We will see how to prevent this in a later unit.Example: Writing logic to test whether 4 > 2Are both data types the same?YesWhat are their data types?intRun 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 type2 is an int data type Convert the data firstConvert.ToInt32(4)Then run the conditionalIf (Convert.ToInt32(4)>2) // then do this;

Converting TextBox data #2Lets say that you want to test a condition, is 2 > 4 (the answer of course is no,2 is not 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 theAnswer 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.And, on the flip sideWhenever 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();Formatting outputToString() 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 ()

Format codesA list of format codes and examples of how they work can be found at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspxFor example:

Common codes:C or c - currencyF or f fixed pointP or p percentage

See the link above for specific examplesUnit 2 L1Time to try it outWe will develop a simple 4 function calculatorBe sure that your ASPPub is on your desktopOpen Visual Web DeveloperOpen the ASPPub web site

L1 #2Right-click on Unit2 under MIS3200 and Add New ItemMake sure Visual C# is selectedSelect Web FormName the form yourLastNameU2L1.aspxMake sure the two boxes are checkedClick AddSelect Site.master from ASPPubClick OK (see next slide)

L1 #312345678L1 #4Switch to Design viewClick in MainContent and type Unit 2 L1 Simple Calculator and then press EnterSelect all the textUse the BlockFormat dropdown list to convert the text to an Heading 2

L1 #5Click on the column to the right of Style in the Properties windowClick to open the Styleeditor windowChange the Background background-color to the same green you used on your master page

L1 #6Change the Font color to WhiteClick OK

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

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

L1 #9Select 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 creates a single cell with the ColSpan property with set to 2)

L1 #10With the merged cell selectedOpen the Style editor (see slide 27 for example)Select Block, text-align to centerAdd a button to the cellChange the (ID) to btnAddChange the Text to +Add three additional buttons and change their properties

(ID)TextbtnSubtract-btnMultiply*btnDivide/L1 #11Notice 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)

L1 #12Merge the two cells in the bottom row and set the text-align to centerAdd a label to the last rowChange the (ID) to lblOutputClear the Text propertyChange Visible to false

L1 #13Double click the + button to create the click event methodAdd appropriate method level comments

After the comments create three decimal variables called decNum1, decNum2 and decSum and a string variable called strOutput

L1 #14Convert the Text in txtNumber1 to a decimal value and store in decNum1Repeat the process for the second number

Then, add the two numbers and store the result in decSumCreate a output message and store it in strOutput (see next slide)

L1 #15

Convert the decimal numbers to strings using the F2 format which displays two decimal digitsA: Concatenate (stick together) the formatted value stored in decNum1 with the string inside the quotes a space f