04 iec t1s1 oops session 05

Upload: caininme

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    1/35

    Slide 1 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    In this session, you will learn to:Use static variables and static functionsIdentify the components of the .NET FrameworkUse the Visual Studio .NET IDE

    Objectives

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    2/35

    Slide 2 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    Static Variables and StaticFunctions

    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.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    3/35

    Slide 3 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    Static Variables

    The keyword ' static ' means that only one instance of agiven 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 staticvariable exists in the memory for all the objects of that class.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    4/35

    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.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    5/35

    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 tocheck how many times a function is called. For the same, hehas been asked to create the function called CountFunction.Help John to create this function.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    6/35

    Slide 6 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    The .NET Framework, introduced by Microsoft, aims atintegrating various programming languages and services.The .NET Framework is designed to make significantimprovements in code reuse, code specialization, resource

    management, multilanguage development, security,deployment, and administration.The .NET Framework consists of all the technologies thathelp in creating and running robust, scalable, anddistributed applications.

    Introducing the .NET Framework

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    7/35Slide 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 indetail.

    Introducing the .NET Framework (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    8/35Slide 8 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    .NET Products:.NET products aim at allowing developers to createapplications, which are capable of interacting seamlessly witheach 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.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    9/35Slide 9 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    .NET Services:.NET helps you to create software as Web services. A WebService is an application or business logic that is accessiblethrough 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, knownas My Services.This service allows users to access data by linking calendars,

    phonebooks, address books, and personal references to thepassport authentication service.

    Introducing the .NET Framework (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    10/35Slide 10 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    The .NET Framework:It is the foundation for designing, developing, and deployingapplications.It is the core of the .NET infrastructure because it exists as alayer between the .NET applications and the underlyingoperating system.

    Let us understand the .NET Framework in detail.

    Introducing the .NET Framework (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    11/35Slide 11 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    The various components of the .NET Framework are:Common Language RuntimeThe .NET Framework Class LibraryUser and Program Interfaces

    Components of the .NET Framework

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    12/35Slide 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

    ConsoleApplications

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    13/35Slide 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 areexecuted.CLR provides services such as code compilation, memoryallocation, and garbage collection.CLR allows the execution of code across different platforms bytranslating code into Intermediate Language (IL).IL is a low level language that the CLR understands.IL is converted into machine language during execution by theJIT compiler. During JIT compilation, code is also checked for

    type safety.Type safety ensures that objects are always accessed in acompatible way.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    14/35Slide 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 thelanguages of the .NET Framework. This set of rules is knownas Common Language Specification (CLS).CLS enables an object or application to interact with theobjects or applications of other languages.

    The classes that follow the rules specified by CLS are termedas CLS-compliant classes. The classes defined in the .NETFramework class library are CLS-compliant.One of the specifications defined in CLS is CTS, whichprovides a type system that is common across all languages.CTS define how data types are declared, used, and managedin the code at run time.While executing the program, CLR :

    Identifies the process of compilationIdentifies the process of code execution

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    15/35Slide 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 .NETlanguage, such as VB.NET, VC++ .NET, and VC#.The .NET Framework class library provides classes that canbe used in the code to accomplish a range of commonprogramming tasks, such as string management, datacollection, database connectivity, and file access.The .NET Framework class library comprises of:

    NamespacesAssemblies

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    16/35Slide 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 FormsWeb FormsConsole Applications

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

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    17/35Slide 17 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    Components of the .NET Framework (Contd.)

    Advantages of the .NET Framework:Consistent programming modelMulti-platform applicationsMulti-language integration

    Automatic resource managementEase of deployment

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    18/35Slide 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 commoninterface 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 .

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    19/35Slide 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 hasprovided two types of containers:

    Project : It typically contains items that make up theapplication. These items are interrelated.Solution : It usually acts as a container for one or moreprojects.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    20/35Slide 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

    MiscellaneousFiles

    Project 1Items

    Project 2Items

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    21/35Slide 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 needto create a project. To create a project, you need to performthe following steps:

    1. Select Start All Programs Microsoft Visual Studio2005 Microsoft Visual Studio 2005 . TheStart Page - Microsoft Visual Studio window will bedisplayed.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    22/35Slide 22 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    2. Select File New Project . The New Project dialog box willbe displayed.

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    23/35Slide 23 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    3. In the New Project dialog box, select Visual C# from theProject Types pane and Console Application from theTemplates 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 tobrowse to the folder in which the new project is to be created.

    6. Click the OK button.

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    24/35

    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 VisualStudio .NET, you can use the following main elements inVisual Studio .NET IDE .

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    25/35

    Slide 25 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

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

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    26/35

    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 itemsof the application

    Cut Places selected text or objects on the WindowsClipboard

    Copy Places a copy of selectedtext or objects on theWindows Clipboard

    Paste Pastes the contents of theClipboard on the document.

    Start Debugging Compiles and Executes thecurrent project.

    Creating Projects and Solutions (Contd.)

    The following table shows the various components of Standard toolbar.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    27/35

    Slide 27 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

    The Start Page:When you start Visual Studio .NET, it displays theStart Page- Microsoft Visual Studio.The Start Page is the default home page for the browser providedwithin 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.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    28/35

    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 projectname, and all the classes that are used in the project.

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    29/35

    Slide 29 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

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

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    30/35

    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 thesource (the file and the line number) of the error. It helps youidentify and locate problems that are detected automatically asyou edit or compile code.

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    31/35

    Slide 31 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

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

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    32/35

    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 usethis editor to add code for your class.

    Creating Projects and Solutions (Contd.)

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    33/35

    Slide 33 of 35Session 5Ver. 1.0

    Object-Oriented Programming Using C#

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

    application.2. Select Debug Start Debugging or press F5 to execute the

    application.

    Compiling and Executing Project

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    34/35

    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 towhich it belongs has been executed.The static functions can access only static variables.Non-static variables cannot be accessed by using staticfunctions. The .NET Framework is made up of many components, suchas Common Language Specification (CLS), Common languageRuntime (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 thecompiler translates the code into the Intermediate Language(IL) instead of machine language.

  • 8/14/2019 04 Iec t1s1 Oops Session 05

    35/35

    Object-Oriented Programming Using C#

    Summary (Contd.)

    The Just-In-Time (JIT) compiler is used to translate code fromIL into machine language.The Common Language Runtime (CLR) is the environmentwhere all .NET applications are executed.The Visual Studio .NET Integrated Development Environment

    (IDE) provides you with a common interface for developingvarious kinds of applications for the .NET Framework.Visual Studio .NET provides two types of containers, projectsand solutions to organize the constituents of an application.