Presentation is loading. Please wait.

Presentation is loading. Please wait.

BİL528 – Bilgisayar Programlama II

Similar presentations


Presentation on theme: "BİL528 – Bilgisayar Programlama II"— Presentation transcript:

1 BİL528 – Bilgisayar Programlama II
Variables

2 Contents Variables

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

4 Visual C# Data Types In Visual C#, there are two categories of data types: Value types Reference types

5 Integral Value Types Type Approx. Range Size byte 0 to 255 1 byte
sbyte –128 to 127 char U+0000 to U+FFFF 2 bytes short –32768 to 32767 ushort 0 to 65535 int –2,147,483,648 to 2,147,483,647 4 bytes uint 0 to 4,294,967,295 long –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 8 bytes ulong 0 to 18,446,744,073,709,551,615

6 Floating Value Types Type Approx. Range Precision Size float
±1.5 × 10−45 to ±3.4 × 1038 7 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 * Use decimal for currencies.

7 Other Value Types Type Explanation bool
Stores Boolean values, true and false enum Enumeration. Example: enum Days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; struct Lightweight version of class.

8 Reference Types Type Explanation string Unicode character arrays
object Everything in C# is inherited from object class directly or indirectly class Blueprint of objects delegate Method signature (like function pointers in C) interface An interface contains only the signatures of methods, properties, events or indexers.

9 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

10 Safe Conversions Type Can Be Safely Converted To byte
short, int, long, float, double, decimal short int, long, float, double, decimal int long, float, double, decimal long float, double, decimal float double decimal

11 Explicitly Converting Data
Simplest way is using C syntax: double d = 2.3; int i = (int) d; You can also use Convert class: int i = Convert.ToInt32(d);

12 Some Common Methods of Convert Class
Converts to ToBoolean bool ToByte byte ToSByte sbyte ToChar char ToDateTime DateTime ToInt16 short ToInt32 int ToInt64 long

13 Some Common Methods of Convert Class
Converts to ToDecimal decimal ToDouble double ToSingle float ToString string ToUInt16 ushort ToUInt32 uint ToUInt64 ulong

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

15 Where to put variables? Put variables in the class definition above the methods

16 Arrays string[] strMyArray; strMyArray = new string[10];
Accessing elements: strMyArray[0] = "BIM211"; strMyArray[1] = "Visual Programming";

17 Multidimensional Arrays
int[,] intMeasurements; intMeasurements = new int[3, 2]; Accessing elements: intMeasurements[0, 0] = 1; intMeasurements[2, 1] = 6;

18 Constants You can define constant variables whose value can not be changed during the program execution by keyword const: const double PI = ;

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

20 FormClosing Event if (m_blnPromptOnExit) { if (MessageBox.Show(“Close the Picture Viewer program?”, “Confirm Exit”, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) e.Cancel = true; }

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

22 String Operations Length SubString() IndexOf()
Trim(), TrimStart(), TrimEnd(), Remove() Replace()

23 Date/Time Operations DateTime dateMyBirthday = new DateTime(2008,7,22); AddDays(), AddHours(), etc. ToLongDateString() ToShortDateString() ToLongTimeString() ToShortTimeString()

24 Date/Time Operations DateTime.Today DateTime.Now


Download ppt "BİL528 – Bilgisayar Programlama II"

Similar presentations


Ads by Google