04 iec t1_s1_oo_ps_session_05

35
Slide 1 of 35 Session 5 Ver. 1.0 Object-Oriented Programming Using C# In this session, you will learn to: Use static variables and static functions Identify the components of the .NET Framework Use the Visual Studio .NET IDE Objectives

Upload: niit-care

Post on 25-May-2015

346 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 04 iec t1_s1_oo_ps_session_05

Slide 1 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

In this session, you will learn to:Use static variables and static functions

Identify the components of the .NET Framework

Use the Visual Studio .NET IDE

Objectives

Page 2: 04 iec t1_s1_oo_ps_session_05

Slide 2 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Static Variables and Static Functions

Each object has its own set of member variables.

To retain the value of a variable throughout the program, you can declare the variable as a static variable.

To manipulate and use the values of static variables, you can define a function as static function.

Page 3: 04 iec t1_s1_oo_ps_session_05

Slide 3 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Static Variables

The keyword 'static' means that only one instance of a given variable exists for a class.

Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.

Static variables can be initialized outside the member function or class definition.

Unlike other member variables, only one copy of the static variable exists in the memory for all the objects of that class.

Page 4: 04 iec t1_s1_oo_ps_session_05

Slide 4 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Static Functions

Static functions can access only static variables.

Static functions exist even before the object is created.

Page 5: 04 iec t1_s1_oo_ps_session_05

Slide 5 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Demo: Counting the Number of Times Function is Called by Using Static Functions

Problem Statement:John a software developer in Zed Axsis Technology needs to check how many times a function is called. For the same, he has been asked to create the function called “CountFunction”. Help John to create this function.

Page 6: 04 iec t1_s1_oo_ps_session_05

Slide 6 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The .NET Framework, introduced by Microsoft, aims at integrating various programming languages and services.

The .NET Framework is designed to make significant improvements in code reuse, code specialization, resource management, multilanguage development, security, deployment, and administration.

The .NET Framework consists of all the technologies that help in creating and running robust, scalable, and distributed applications.

Introducing the .NET Framework

Page 7: 04 iec t1_s1_oo_ps_session_05

Slide 7 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The .NET suite consists of:.NET Products

.NET Services

.NET Framework

Let us understand each component of the .NET suite in detail.

Introducing the .NET Framework (Contd.)

Page 8: 04 iec t1_s1_oo_ps_session_05

Slide 8 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

.NET Products:.NET products aim at allowing developers to create applications, which are capable of interacting seamlessly with each other.

All .NET products use eXtensible Markup Language (XML) for describing and exchanging data between applications.

An example of a .NET product is Visual Studio .NET.

Introducing the .NET Framework (Contd.)

Page 9: 04 iec t1_s1_oo_ps_session_05

Slide 9 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

.NET Services:.NET helps you to create software as Web services. A Web Service is an application or business logic that is accessible through standard Internet protocols such as Hypertext Transfer Protocol (HTTP) and Simple Object Access Protocol (SOAP). You can identify the service by a Uniform Resource Locator (URL).

Microsoft has come up with its own set of Web services, known as My Services.

This service allows users to access data by linking calendars, phonebooks, address books, and personal references to the passport authentication service.

Introducing the .NET Framework (Contd.)

Page 10: 04 iec t1_s1_oo_ps_session_05

Slide 10 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The .NET Framework:It is the foundation for designing, developing, and deploying applications.

It is the core of the .NET infrastructure because it exists as a layer between the .NET applications and the underlying operating system.

Let us understand the .NET Framework in detail.

Introducing the .NET Framework (Contd.)

Page 11: 04 iec t1_s1_oo_ps_session_05

Slide 11 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The various components of the .NET Framework are:Common Language Runtime

The .NET Framework Class Library

User and Program Interfaces

Components of the .NET Framework

Page 12: 04 iec t1_s1_oo_ps_session_05

Slide 12 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The following figure displays the various components of the .NET Framework.

Components of the .NET Framework (Contd.)

Common Language Runtime (CLR)

.NET Framework Class Libraries

WindowsForms

Web Forms andWeb Services

Console Applications

Page 13: 04 iec t1_s1_oo_ps_session_05

Slide 13 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Components of the .NET Framework (Contd.)

Common Language Runtime (CLR):CLR is the environment where all programs in .NET are executed.

CLR provides services such as code compilation, memory allocation, and garbage collection.

CLR allows the execution of code across different platforms by translating code into Intermediate Language (IL).

IL is a low level language that the CLR understands.

IL is converted into machine language during execution by the JIT compiler. During JIT compilation, code is also checked for type safety.

Type safety ensures that objects are always accessed in a compatible way.

Page 14: 04 iec t1_s1_oo_ps_session_05

Slide 14 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Components of the .NET Framework (Contd.)

CLR consists of a set of common rules followed by all the languages of the .NET Framework. This set of rules is known as Common Language Specification (CLS).

CLS enables an object or application to interact with the objects or applications of other languages.

The classes that follow the rules specified by CLS are termed as CLS-compliant classes. The classes defined in the .NET Framework class library are CLS-compliant.

One of the specifications defined in CLS is CTS, which provides a type system that is common across all languages. CTS define how data types are declared, used, and managed in the code at run time.

While executing the program, CLR:Identifies the process of compilation

Identifies the process of code execution

Page 15: 04 iec t1_s1_oo_ps_session_05

Slide 15 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Components of the .NET Framework (Contd.)

The .NET Framework Class Library:The .NET Framework class library works with any .NET language, such as VB.NET, VC++ .NET, and VC#.

The .NET Framework class library provides classes that can be used in the code to accomplish a range of common programming tasks, such as string management, data collection, database connectivity, and file access.

The .NET Framework class library comprises of:Namespaces

Assemblies

Page 16: 04 iec t1_s1_oo_ps_session_05

Slide 16 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Components of the .NET Framework (Contd.)

User and Program Interfaces:At the presentation layer, .NET provides three types of user interfaces:

Windows Forms

Web Forms

Console Applications

.NET provides a program interface, Web Services, to communicate with remote components.

Page 17: 04 iec t1_s1_oo_ps_session_05

Slide 17 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Components of the .NET Framework (Contd.)

Advantages of the .NET Framework:Consistent programming model

Multi-platform applications

Multi-language integration

Automatic resource management

Ease of deployment

Page 18: 04 iec t1_s1_oo_ps_session_05

Slide 18 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Using Visual Studio .NET IDE

The Visual Studio .NET IDE provides you with a common interface for developing various kinds of projects for the .NET Framework.

The IDE also provides you with a centralized location for designing the user interface for an application, writing code, and compiling and debugging the application.

Page 19: 04 iec t1_s1_oo_ps_session_05

Slide 19 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Creating Projects and Solutions

In Visual Studio .NET, an application can be made up of one or more items, such as files and folders.

To organize these items efficiently, Visual Studio .NET has provided two types of containers:

Project: It typically contains items that make up the application. These items are interrelated.

Solution: It usually acts as a container for one or more projects.

Page 20: 04 iec t1_s1_oo_ps_session_05

Slide 20 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Creating Projects and Solutions (Contd.)

The following figure shows a solution with multiple projects.

Solution

Project 1

Project 2

Miscellaneous Files

Project 1Items

Project 2Items

Page 21: 04 iec t1_s1_oo_ps_session_05

Slide 21 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Creating Projects and Solutions (Contd.)

To create a console application in Visual Studio, you need to create a project. To create a project, you need to perform the following steps:1. Select Start All Programs Microsoft Visual Studio

2005 Microsoft Visual Studio 2005. The Start Page - Microsoft Visual Studio window will be displayed.

Page 22: 04 iec t1_s1_oo_ps_session_05

Slide 22 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

2. Select FileNewProject. The New Project dialog box will be displayed.

Creating Projects and Solutions (Contd.)

Page 23: 04 iec t1_s1_oo_ps_session_05

Slide 23 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

3. In the New Project dialog box, select Visual C# from the Project Types pane and Console Application from the Templates pane.

4. Specify the name of the application in the Name text box.

5. Specify the location where the new project is to be created in the Location combo box. You can use the Browse button to browse to the folder in which the new project is to be created.

6. Click the OK button.

Creating Projects and Solutions (Contd.)

Page 24: 04 iec t1_s1_oo_ps_session_05

Slide 24 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Creating Projects and Solutions (Contd.)

User Interface Elements of Visual Studio .NET IDE: When you work with a console application project in Visual Studio .NET, you can use the following main elements in Visual Studio .NET IDE.

Page 25: 04 iec t1_s1_oo_ps_session_05

Slide 25 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Standard Toolbar:The Standard toolbar is located below the menu bar. It provides shortcuts for menu commands.

Creating Projects and Solutions (Contd.)

Page 26: 04 iec t1_s1_oo_ps_session_05

Slide 26 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Name Button Function

NewProject Creates a new project

Add New Item Add new item in the project

Save Saves the Program.

SaveAll Save all the unsaved items of the application

Cut Places selected text or objects on the Windows Clipboard

Copy Places a copy of selected text or objects on the Windows Clipboard

Paste Pastes the contents of the Clipboard on the document.

Start Debugging Compiles and Executes the current project.

Creating Projects and Solutions (Contd.)

The following table shows the various components ofStandard toolbar.

Page 27: 04 iec t1_s1_oo_ps_session_05

Slide 27 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Start Page:When you start Visual Studio .NET, it displays the Start Page- Microsoft Visual Studio.

The Start Page is the default home page for the browser provided within the Visual Studio .NET IDE.

This tab displays some of the recent projects and the last date of their modification. You can open any one of these projects.

Creating Projects and Solutions (Contd.)

Page 28: 04 iec t1_s1_oo_ps_session_05

Slide 28 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Solution Explorer Window:The Solution Explorer window lists the solution name, the project name, and all the classes that are used in the project.

Creating Projects and Solutions (Contd.)

Page 29: 04 iec t1_s1_oo_ps_session_05

Slide 29 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Output Window:The Output window displays messages for the status of various features provided in the Visual Studio .NET IDE.

Creating Projects and Solutions (Contd.)

Page 30: 04 iec t1_s1_oo_ps_session_05

Slide 30 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Error List Window:The Error List window displays a list of errors along with the source (the file and the line number) of the error. It helps you identify and locate problems that are detected automatically as you edit or compile code.

Creating Projects and Solutions (Contd.)

Page 31: 04 iec t1_s1_oo_ps_session_05

Slide 31 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Class View Window:The Class View window displays the classes, methods, and properties associated with a particular file. They are displayed in a hierarchical tree-view depicting the containership of these items.

Creating Projects and Solutions (Contd.)

Page 32: 04 iec t1_s1_oo_ps_session_05

Slide 32 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

The Code Editor:The code editor allows you to enter and edit code. You may use this editor to add code for your class.

Creating Projects and Solutions (Contd.)

Page 33: 04 iec t1_s1_oo_ps_session_05

Slide 33 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

To compile and execute the application, you need to perform the following steps:1. Select BuildBuild Solution or press F6 to compile the

application.

2. Select DebugStart Debugging or press F5 to execute the application.

Compiling and Executing Project

Page 34: 04 iec t1_s1_oo_ps_session_05

Slide 34 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Summary

In this session, you learned that:The static variable retains its value even after the function to which it belongs has been executed.

The static functions can access only static variables. Non-static variables cannot be accessed by using static functions.

The .NET Framework is made up of many components, such as Common Language Specification (CLS), Common language Runtime (CLR), and Just-In-Time (JIT) compiler.

CLS is a set of rules that are followed by all the languages of the .NET Framework.

When a program is compiled using Visual Studio .NET the compiler translates the code into the Intermediate Language (IL) instead of machine language.

Page 35: 04 iec t1_s1_oo_ps_session_05

Slide 35 of 35Session 5Ver. 1.0

Object-Oriented Programming Using C#

Summary (Contd.)

The Just-In-Time (JIT) compiler is used to translate code from IL into machine language.

The Common Language Runtime (CLR) is the environment where all .NET applications are executed.

The Visual Studio .NET Integrated Development Environment (IDE) provides you with a common interface for developing various kinds of applications for the .NET Framework.

Visual Studio .NET provides two types of containers, projects and solutions to organize the constituents of an application.