24
BİL528 – Bilgisayar Programlama II Variables 1

BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Embed Size (px)

Citation preview

Page 1: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

BİL528 – Bilgisayar Programlama II

Variables

1

Page 2: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Contents

• Variables

2

Page 3: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Variables

• In your programs, you must determine the type of your data (int, string, etc.)

• In object oriented programming languages, you must also determine the level of visibility of your data (public, private, etc).

• This visibility is known as scope.

3

Page 4: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Visual C# Data Types

• In Visual C#, there are two categories of data types:– Value types– Reference types

4

Page 5: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Integral Value TypesType Approx. Range Sizebyte 0 to 255 1 bytesbyte –128 to 127 1 bytechar U+0000 to U+FFFF 2 bytesshort –32768 to 32767 2 bytesushort 0 to 65535 2 bytesint –2,147,483,648 to 2,147,483,647 4 bytesuint 0 to 4,294,967,295 4 byteslong –9,223,372,036,854,775,808 to

9,223,372,036,854,775,8078 bytes

ulong 0 to 18,446,744,073,709,551,615 8 bytes5

Page 6: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Floating Value Types

Type Approx. Range Precision Sizefloat ±1.5 × 10−45 to

±3.4 × 10387 digits 32 bits

double ±5.0 × 10−324 to ±1.7 × 10308

15-16 digits 64 bits

decimal ±1.0 × 10−28 to ±7.9 × 1028

28-29 digits 128 bits

6

* Use decimal for currencies.

Page 7: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Other Value Types

Type Explanationbool Stores Boolean values, true and falseenum Enumeration. Example:

enum Days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

struct Lightweight version of class.

7

Page 8: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Reference TypesType Explanationstring Unicode character arraysobject Everything in C# is inherited from object class

directly or indirectlyclass Blueprint of objectsdelegate Method signature (like function pointers in C)interface An interface contains only the signatures

of methods, properties, events or indexers.

8

Page 9: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Type Casting

• There are two types of type casting: Implicit and Explicit• Implicit conversions are done automatically by the

compiler– Widening cast– No data loss– Example: conversion from float to double

• Explicit conversions require programmers approval– Narrowing cast– Data may be lost– Example: conversion from double to float

9

Page 10: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Safe Conversions

Type Can Be Safely Converted Tobyte short, int, long, float, double, decimalshort int, long, float, double, decimalint long, float, double, decimallong float, double, decimalfloat doubledouble decimal

10

Page 11: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Explicitly Converting Data

• Simplest way is using C syntax:– double d = 2.3;– int i = (int) d;

• You can also use Convert class:– double d = 2.3;– int i = Convert.ToInt32(d);

11

Page 12: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Some Common Methods of Convert Class

Method Converts toToBoolean boolToByte byteToSByte sbyteToChar charToDateTime DateTimeToInt16 shortToInt32 intToInt64 long

12

Page 13: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Some Common Methods of Convert Class

Method Converts toToDecimal decimalToDouble doubleToSingle floatToString stringToUInt16 ushortToUInt32 uintToUInt64 ulong

13

Page 14: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Declaring Variables

• datatype variable_name = initial_value;– int a;– string str = "BIM211";– double m = 10, n = 20;– long k, l = 100;

• Visual C# is a strongly typed language; therefore, you must always declare the data type of a variable.

• In addition, Visual C# requires that all variables be initialized before they’re used.

14

Page 15: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Where to put variables?

• Put variables in the class definition above the methods

15

Page 16: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Arrays

• string[] strMyArray;• strMyArray = new string[10];

• Accessing elements:• strMyArray[0] = "BIM211";• strMyArray[1] = "Visual Programming";

16

Page 17: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Multidimensional Arrays

• int[,] intMeasurements;• intMeasurements = new int[3, 2];

• Accessing elements:• intMeasurements[0, 0] = 1;• intMeasurements[2, 1] = 6;

17

Page 18: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Constants

• You can define constant variables whose value can not be changed during the program execution by keyword const:

• const double PI = 3.14159;

18

Page 19: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Exercise

• Create a boolean variable to store whether a confirmation box appears or not

• Change its value when “Confirm on exit” menu item is clicked

• Handle the FormClosing event and write the following code:

• (see next slide)

19

Page 20: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

FormClosing Event

if (m_blnPromptOnExit){ if (MessageBox.Show(“Close the Picture Viewer

program?”, “Confirm Exit”, MessageBoxButtons.YesNo, MessageBoxIcon.Question)

== DialogResult.No) { e.Cancel = true; }}

20

Page 21: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Arithmetic Operations

• Arithmetic operations are same as C but you may prefer to read chapter 12 of the textbook.

21

Page 22: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

String Operations

• Length• SubString()• IndexOf()• Trim(), TrimStart(), TrimEnd(), Remove()• Replace()

22

Page 23: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Date/Time Operations

• DateTime dateMyBirthday = new DateTime(2008,7,22);

• AddDays(), AddHours(), etc.• ToLongDateString()• ToShortDateString()• ToLongTimeString()• ToShortTimeString()

23

Page 24: BİL528 – Bilgisayar Programlama II Variables 1. Contents Variables 2

Date/Time Operations

• DateTime.Today• DateTime.Now

24