48
MIS 3200 – Unit 2.1 • Outline – Variables – Arithmetic – Formatting – Conversion

MIS 3200 – Unit 2.1

  • Upload
    sydney

  • View
    51

  • Download
    1

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

Citation preview

Page 1: MIS 3200  – Unit 2.1

MIS 3200 – Unit 2.1

• Outline – Variables– Arithmetic– Formatting– Conversion

Page 2: MIS 3200  – Unit 2.1

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.

Page 3: MIS 3200  – Unit 2.1

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

Page 4: MIS 3200  – Unit 2.1

Data TypesData Type Signed Bytes RangeShort Yes 2 -32,768 to 32,767int Yes 4 -2,147,483,648 to 2,147,483,647Int32 Yes 4 -2,147,483,648 to 2,147,483,647long Yes 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Int64 Yes 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807double Yes 8 Approx ±5.0 x 10-324 to ±1.7 x 10308 with 15 or 16 sig figuresdecimal Yes 12 Approx ±10-28 to ±7.9 x 1028 with 28 or 29 significant figureschar N/A 2 Any single Unicode character (16 bit)bool N/A 1 / 2 true or falsestring N/A .. Text

Commonly used data types are highlighted

Page 5: MIS 3200  – Unit 2.1

• 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

Usage Rules

Page 6: MIS 3200  – Unit 2.1

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.

Page 7: MIS 3200  – Unit 2.1

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; – int intNumWorkDays = 6;

Page 8: MIS 3200  – Unit 2.1

Usage Examples

Int32 intMonths = 12; int intNumStudents = 25548612;string strUserName = “Matta”;String strPassword = “Vic”;decimal decAnnualSavings = 12,456.00m;decimal decSavingsPerMonth

= decAnnualSavings/intMonths;

Page 9: MIS 3200  – Unit 2.1

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

Page 10: MIS 3200  – Unit 2.1

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)

Page 11: MIS 3200  – Unit 2.1

Assigning Variable - 2

• Variables 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 side

Page 12: MIS 3200  – Unit 2.1

Math operations

• C# uses the set of arithmetic operators common to most computer languages

Arithmetic operation C# operator

Addition +

Subtraction -

Multiplication *

Division /

Modulo (remainder) %

Page 13: MIS 3200  – Unit 2.1

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

Page 14: MIS 3200  – Unit 2.1

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

Page 15: MIS 3200  – Unit 2.1

Convert

• The previous error message said it couldn’t 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 this

Page 16: MIS 3200  – Unit 2.1

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

Page 17: MIS 3200  – Unit 2.1

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.

Page 18: MIS 3200  – Unit 2.1

Example: Writing logic to test whether 4 > 2

1. Are both data types the same?– Yes

2. What are their data types?– int

3. Run the conditional statement:– If (4>2)

// then do this;

1. Are both data types the same?– No (assume “4” with 4)

2. What are their data types?– “4” is a String data type– 2 is an int data type

3. Convert the data first– Convert.ToInt32(“4”)

4. Then run the conditional– If (Convert.ToInt32(“4”)>2)

// then do this;

Page 19: MIS 3200  – Unit 2.1

Converting TextBox data #2

Let’s 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.

Page 20: MIS 3200  – Unit 2.1

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 string

lblOutput.Text = decSalesTaxRate.ToString();

Page 21: MIS 3200  – Unit 2.1

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 ()

Page 22: MIS 3200  – Unit 2.1

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

Page 23: MIS 3200  – Unit 2.1

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

Page 24: MIS 3200  – Unit 2.1

L1 #2

1. Right-click on Unit2 under MIS3200 and Add New Item2. Make sure Visual C# is selected3. Select Web Form4. Name the form yourLastNameU2L1.aspx5. Make sure the two boxes are checked6. Click Add7. Select Site.master from ASPPub8. Click OK (see next slide)

Page 25: MIS 3200  – Unit 2.1

L1 #31

2 3

4 5

6

7

8

Page 26: MIS 3200  – Unit 2.1

L1 #4

9. Switch to Design view10. Click in MainContent and type Unit 2 L1 – Simple

Calculator and then press Enter11. Select all the text12. Use the BlockFormat dropdown

list to convert the text to an Heading 2

Page 27: MIS 3200  – Unit 2.1

L1 #5

13. Click on the column to the right of Style in the Properties window

14. Click to open the Styleeditor window

15. Change the Background background-color to the same green you used on your master page

Page 28: MIS 3200  – Unit 2.1

L1 #616 Change the

Font color to White

17 Click OK

Page 29: MIS 3200  – Unit 2.1

L1 #7

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

Page 30: MIS 3200  – Unit 2.1

L1 #819 Type Number 1 in row 1, col 120 Type Number 2 in row 2, col 121 Add a TextBox to row 1, col 2,

and change the (ID) to txtNumber122 Add a TextBox to row 2, col 2

and change the (ID) to txtNumber2

Page 31: MIS 3200  – Unit 2.1

L1 #923 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

24 From the Table menu, select Modify and Merge Cells (this creates a single cell with the ColSpan

property with set to 2)

Page 32: MIS 3200  – Unit 2.1

L1 #10

25 With the merged cell selected– Open the Style editor (see slide 27 for example)

– Select Block, text-align to center26 Add a button to the cell– Change the (ID) to btnAdd– Change the Text to +

27 Add three additional buttons and change their properties (ID) Text

btnSubtract -

btnMultiply *

btnDivide /

Page 33: MIS 3200  – Unit 2.1

L1 #11

• Notice that the buttons have slightly different sizes. It is a good design principle to keep related buttons the same size.

28 Select each button and change the Width property to 30px (px means pixel)

Page 34: MIS 3200  – Unit 2.1

L1 #12

29 Merge the two cells in the bottom row and set the text-align to center

30 Add a label to the last row– Change the (ID) to lblOutput– Clear the Text property– Change Visible to false

Page 35: MIS 3200  – Unit 2.1

L1 #1331 Double click the + button to create the click event method32 Add appropriate method level comments

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

Page 36: MIS 3200  – Unit 2.1

L1 #14

34 Convert the Text in txtNumber1 to a decimal value and store in decNum1

35 Repeat the process for the second number

36 Then, add the two numbers and store the result in decSum

37 Create a output message and store it in strOutput (see next slide)

Page 37: MIS 3200  – Unit 2.1

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

B: Concatenate the formatted version of decNum2 to the string created in step A

C: Concatenate the results of step B with the string inside the quotes – a space followed by a = followed by a space

D: Concatenate the formatted version of decSum to the string created in step C

Page 38: MIS 3200  – Unit 2.1

L1 #1638 Assign the formatted output to lblOutput39 Change the visibility of lblOutput to true

40 Click in the Page_Load method41 Set focus

to txtNumber142 Add appropriate page level comments

Page 39: MIS 3200  – Unit 2.1

L1 #17

43 Save everything44 Open the U2L1 aspx file in Design mode45 Click Run46 Enter two numbers such as 55 and 2747 Click the + button48 You should see …

Page 40: MIS 3200  – Unit 2.1

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

Page 41: MIS 3200  – Unit 2.1

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 page

i.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

Page 42: MIS 3200  – Unit 2.1

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

Page 43: MIS 3200  – Unit 2.1

Copy L1 and rename L2

MIS3200

MIS3200

MIS3200

Page 44: MIS 3200  – Unit 2.1

L2 #1

1. Open the newly copied L2 aspx file in Design view2. Change the heading to say Unit 2 L23. We have one clean-up detail leftover from L1 – we

should clear the contents of the textboxes after the button is clickeda. 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

Page 45: MIS 3200  – Unit 2.1

L2 #2

4. Return to the U2L2 aspx file in design view5. Double-click the – button to create the click event

method for Subtraction6. Add comments similar to what you had for Addition7. Using btnAdd_Click as a pattern

a. write code to subtract number2 from number1b. display the results (see slide 38, step 39 for an example)

c. make any necessary changes to convert addition to subtraction and use appropriate variable names

Page 46: MIS 3200  – Unit 2.1

L2 #3• Your code should look something like this

• And produce output like this

Page 47: MIS 3200  – Unit 2.1

L2 #4

8. Repeat the process for the * and the / buttons, including the comments

9. Be sure to test all functions10. 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

11. Put your ASPPub back on ASPNET and submit – your MIS Portfolio URL to the drop box– Your L2 page URL to the dropbox

Page 48: MIS 3200  – Unit 2.1

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()