0% found this document useful (0 votes)
8 views31 pages

Event Driven Programming- Lecture 2

Uploaded by

epohulll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views31 pages

Event Driven Programming- Lecture 2

Uploaded by

epohulll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Event Driven Programming

Lecture 2
Object Oriented Programming
Class

Application

Class Class Class

Class Class Class

Class Class

Class Class Class

Class Class
Class

Presentation Layer

Business Logic Layer

Data Layer
Object

- An instance of a class

Car
Car1
Make
Model Car2
Color

Start() Car3
Drive()
Stop()
Syntax
public class ClassName

// class members

}
Syntax
public class ClassName
{
public datatype FieldName;
public returntype MethodName()
{
// method body
}
}
Demo: Create a Customer Class
Static Members of a Class

- Represent concepts in objects that are singletons


Demo: Add a createCustomer method
Constructors

- A method that is called when an instance of a class is created


- Used to create an object in an early state
- Needs to have the exact same name as the class
- Has no return types (not even void)
Syntax
public class ClassName

public ClassName()

}
Demo: Create a Customer Constructor
Constructor Overloading
Public VS Private
Can a constructor be private?
C# Properties (Get and Set)
- More on getters and setters
C# Property
A property is like a combination of a variable and a method, and it has two methods: a get and a set
method.

class ClassName
{
private string field;
public string Field
{
get { return field; } // get method
set { field = value; } // set method
}
}
C# Property

class ClassName

// private string field;

public string Field { get; set;}

}
Generics

Why do we use them?


How?
Build a DTO
Exception

a problem that arises during the execution of a program.


Defined in the System.Exception class
Exception Handling

try - where you place “dangerous” code


catch - handles exceptions when they occur
finally - always executes whether or not an error occurs
throw - allows you to create a custom error
Demo: Add a setAge(str) method to Customer
Demo: Construct a class called NotificationService
Customer

NotificationService
Events

A mechanism for inter-process communication.


Helps to build loosely coupled applications
Customer
NotificationService

SomeOtherService
Customer
NotificationService
Publisher/ Emitter
Subscriber / Receiver
How to

1- Define a delegate
2- Define an event
3- Raise the event

4- Add event handler

Customer 5- Subscribe to the event

NotificationService

You might also like