Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types, Operators, and
Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods

Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into

Department of CSE, QUEST


Dr. Irfana Memon
Collections, Handling Events, Defining and Implementing Interfaces
Creating a Class Hierarchy by Using Inheritance, Extending .NET Framework Classes,
Creating Generic Types
Accessing a Database: Creating and Using Entity Data Models, Querying and Updating Data
by Using LINQ
Designing the User Interface for a Graphical Application: Using XAML, Binding Controls to
Data, Styling a User Interface
Improving Application Performance and Responsiveness: Implementing Multitasking by
using Tasks and Lambda Expressions
2
Performing Operations Asynchronously, Synchronizing Concurrent Access to Data
Implementing Type-
safe Collections:
Creating classes,
Organizing Data into

Department of CSE, QUEST


Dr. Irfana Memon
Collections, Handling
Events, Defining and
implementing
interfaces 3
Classes and Object in C#
• One can create new reference types by defining classes.
• Classes provide templates’ from which these direct instances
are generated.
• When you define a class, you define a blueprint for a data
type.

Department of CSE, QUEST


Dr. Irfana Memon
4
What are classes?

• Classes are a core feature of Object Orientated Programming


(OOP) languages.
• They are a logical grouping of methods and properties
representing and encapsulating a problem concept.
• You've already met a class before, the Program class:

Department of CSE, QUEST


Dr. Irfana Memon
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
} 5
}
How do create classes?
• Classes are declared in C# using the class keyword.
• As you can see in the example below, we've declared a class
called Person .

using System;

Department of CSE, QUEST


Dr. Irfana Memon
public class Program
{
public static void Main()
{
}
} 6
How do create classes?
• Classes are declared in C# using the class keyword.
• As you can see in the example below, we've declared a class
called Person .

using System;
public class Person

Department of CSE, QUEST


Dr. Irfana Memon
{

public class Program


{
public static void Main()
{ 7
}
}
Exercise
Define your own class named Animal with no methods or
properties.

Department of CSE, QUEST


Dr. Irfana Memon
8
Exercise
Define your own class named Animal with no methods or
properties.

using System;

public class Animal


{

Department of CSE, QUEST


Dr. Irfana Memon
}

public class Program


{
public static void Main()
{

}
}
9
Members Of Class
Class is a mechanism to implement the encapsulation, it bind the data and a function
in a single unit. Therefore data and functions are the members of class, which is
known as member data and member function.
There is an example of full class:

class circle
{

Department of CSE, QUEST


Dr. Irfana Memon
double radius;
public void get_radius(double r)
{
radius = r;
}
public double area()
{
return ( 3.14 * r * r);
}
} 10
Member Access Modifiers

• Access modifiers provide the accessibility control for the members


of classes to outside the class.
• They also provide the concept of data hiding.
• There are five member access modifiers provided by the C#
Language.

Department of CSE, QUEST


Dr. Irfana Memon
By default all members of class have private accessibility. If we want a
member to have any other accessibility, then we must specify a 11
suitable access modifier to it individually.
Defining a Class (Example)
using System;
namespace BoxApplication
{
class Box
{
public double length; // Length of a box
public double breadth; // Breadth of a box
public double height; // Height of a box

Department of CSE, QUEST


Dr. Irfana Memon
}

12
Defining a Class (Example)
class Boxtester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;

Department of CSE, QUEST


Dr. Irfana Memon
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification // volume of box 1
Box2.height = 10.0; volume = Box1.height * Box1.length * Box1.breadth;
Box2.length = 12.0; Console.WriteLine("Volume of Box1 : {0}", volume);
Box2.breadth = 13.0; // volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
13
}
}
}
C# Constructors

• Constructor is method of class to allocate the memory of


object and initialize the variables of class.
• When you declare a variable of a class, a special method
must be called to initialize the members of that class.
• This method is automatically provided for every class and it

Department of CSE, QUEST


Dr. Irfana Memon
is called a constructor.

• A default constructor does not have any parameter but if


you need, a constructor can have parameters.
• Such constructors are called parameterized constructors.
• This technique helps you to assign initial value to an object
at the time of its creation.
14
C# Destructor
• A destructor is a special member function of a class that is
executed whenever an object of its class goes out of scope.
• A destructor has exactly the same name as that of the class
with a prefixed tilde (~) and it can neither return a value nor
can it take any parameters.
• Destructor can be very useful for releasing memory

Department of CSE, QUEST


Dr. Irfana Memon
resources before exiting the program.
• Destructors cannot be inherited or overloaded.

15
Exercise : creating class with
instance method, constructor
and destructor

Department of CSE, QUEST


Dr. Irfana Memon
16
// Namespace Declaration
using System;

// Program start class


class ExampleClass
{
// Main begins program execution.
public static void Main()
{

Department of CSE, QUEST


Dr. Irfana Memon
}
}

17
// Namespace Declaration
using System;

// helper class
class OutputClass
{
string myString;

Department of CSE, QUEST


Dr. Irfana Memon
// Program start class
class ExampleClass
{
// Main begins program execution.
public static void Main()
{

18
}
// Namespace Declaration
using System; // Program start class
class ExampleClass
// helper class {
// Main begins program execution.
class OutputClass
public static void Main()
{
{
string myString;

// Constructor }
public OutputClass(string inputString) }
{

Department of CSE, QUEST


Dr. Irfana Memon
myString = inputString;
}

19
// Namespace Declaration
using System; // Program start class
class ExampleClass
// helper class {
class OutputClass // Main begins program execution.
public static void Main()
{
{
string myString;

// Constructor }
public OutputClass(string inputString) }
{

Department of CSE, QUEST


Dr. Irfana Memon
myString = inputString;
}

// Instance Method
public void printString()
{
Console.WriteLine("{0}", myString);
}
20
}
// Namespace Declaration
using System; // Program start class
// helper class class ExampleClass
class OutputClass {
// Main begins program execution.
{
public static void Main()
string myString; {
// Constructor
public OutputClass(string inputString)
{ }
myString = inputString; }

Department of CSE, QUEST


Dr. Irfana Memon
}
// Instance Method
public void printString()
{
Console.WriteLine("{0}", myString);
}
// Destructor
~OutputClass()
{
21
// Some resource cleanup routines
}
}
// Namespace Declaration
using System; // Program start class
// helper class class ExampleClass
class OutputClass {
// Main begins program execution.
{
public static void Main()
string myString; {
// Constructor // Instance of OutputClass
public OutputClass(string inputString) OutputClass outCl = new
{ OutputClass("This is printed by the
myString = inputString; output class.");
// Call Output class' method

Department of CSE, QUEST


Dr. Irfana Memon
}
// Instance Method outCl.printString();
public void printString()
}
{
}
Console.WriteLine("{0}", myString);
}
// Destructor
~OutputClass()
{
22
// Some resource cleanup routines
}
}
C# Exception Handling
• An exception is a problem that arises during the execution
of a program.
• A C# exception is a response to an exceptional
circumstance that arises while a program is running, such
as an attempt to divide by zero.
• Exceptions provide a way to transfer control from one part

Department of CSE, QUEST


Dr. Irfana Memon
of a program to another.
• C# exception handling is built upon four keywords:
 Try
 Catch
 Finally
 throw.
23
C# Exception Handling
Syntax
• Assuming a block raises an exception, a method catches an
exception using a combination of the try and catch
keywords.
• A try/catch block is placed around the code that might
generate an exception.

Department of CSE, QUEST


Dr. Irfana Memon
• Code within a try/catch block is referred to as protected
code, and the syntax for using try/catch looks like the
following −

24
Wish You Good Luck

Dr. Irfana Memon


25

Department of CSE, QUEST

You might also like