Event-Driven Programming
Event-Driven Programming
Tutorial – class
By: Dires B.
Outlines
⁃ Overview of Event-Driven Programming
⁃ Overview of .NET Framework
⁃ Object Oriented Programming
⁃ File Handling
⁃ ADO.NET (ActiveX Data Object)
What is Event-Driven Programming?
• Event-driven programming (EDP) is revolutionizing the
software development industry and is increasingly being used
in modern development.
• Its spread was stimulated by Windows and the dissemination
of visual RAD environments.
• As its name suggests, the programming approach focuses on
events. The latter may be user-initiated, systemic, and
program-generated.
• The most common areas of EDP application today include
the creation of GUIs, server apps, and multi-player game
development.
• Unlike old-style programs controlling the user’s options and
predetermining the flow of events, more adaptive and
innovative programming came up with a graphical user
interface (GUI).
Cont..
• The GUI changed the human-computer interaction by giving
users multiple options in the form of drop-down menus,
windows, buttons, checkboxes, etc. As a result, people gained
the ability to select one of the many possible instructions on
their own rather than having to follow the planned order of
activities set by the computer.
What is .NET Framework?
• .NET Framework is a software development platform
developed by Microsoft for building and running Windows
applications.
• The .NET framework consists of developer tools,
programming languages, and libraries to build desktop and
web applications. It is also used to build websites, web
services, and games.
• The Microsoft .NET framework can be used to create both –
Form-based and Web-based applications. Web services can
also be developed using the .NET framework.
.NET Framework Architecture
• .NET Framework Architecture is a programming model for
the .NET platform that provides an execution environment
and integration with various programming languages for
simple development and deployment of various Windows
and desktop applications.
Object Oriented Programming(OOP)
What is OOP?
• Procedural programming is about writing procedures or
methods that perform operations on the data, while OOP is
about creating objects that contain both data and methods.
• OOP has several advantages over procedural programming:
⁃ OOP is faster and easier to execute
⁃ OOP provides a clear structure for the programs
⁃ OOP helps to keep the C# code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
⁃ OOP makes it possible to create full reusable applications with less
code and shorter development time
• The "Don't Repeat Yourself" (DRY) principle is about
reducing the repetition of code. You should extract out the
codes that are common for the application, and place them at
a single place and reuse them instead of repeating it.
Classes and Objects
• A Class is like an object constructor, or a "blueprint" for creating
objects.
• Everything in C# is associated with classes and objects, along with
its attributes and methods.
• For example: in real life, a car is an object. The car has attributes,
such as weight and color, and methods, such as drive and brake.
Example:
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
When a variable is declared directly in a class, it is often referred to
as a field (or attribute).
Note that we use the dot syntax (.) to access variables/fields inside a
class (myObj.color).
Class Members
• Fields and methods inside classes are often referred to as
"Class Members":
• Example Create a Car class with three class members:
two fields and one method.
Example:
class Car
{
string color = "red"; //field
int speed = 200; //field
public void displayInfo() //method
{
Console.Writeline(“The Car is going as fast as it can “);
}
}
Constructors
• A constructor is a special method that is used to initialize objects.
• The advantage of a constructor, is that it is called when an object
of a class is created. It can be used to set initial values for fields.
Example:
class Car
{
public string model; //field
int speed = 200; //field
public Car() //defining constructor
{
model = “Mustang”;
}
static void Main(string[] args)
{
Car Ford = new Car();// Creating of object of the Car and
// instantiate the object by the constructor
Console.WriteLine(Ford.model);//accessing of the field model
}
Access Modifiers
• C# has the following access modifiers:
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
The code is accessible within the same class, or in a class
protected
that is inherited from that class.
class Car
{
private string model = "Mustang";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
Note the field “model” didn’t accessible out of Car class.
Because the field is declared in the access modifier private.
Basic of OOP
Encapsulation: in object oriented programming
methodology, prevents access to implementation details.
Inheritance: It is useful for code reusability: reuse fields
and methods of an existing class when you create a new
class.
Polymorphism: can be static (Method overloading) or
dynamic (Method Overriding). In static polymorphism, the
response to a function is determined at the compile time. In
dynamic polymorphism, it is decided at run-time.
Abstraction : allows making relevant information visible and hide
detail implementation.
Interface : contain only the declaration of the members. It is
the responsibility of the deriving class to define the members.
Summery
• Object-Oriented Programming (OOP) is a programming
paradigm in computer science that relies on the concept of
classes and objects.
• It is used to structure a software program into simple,
reusable pieces of code blueprints (usually called classes),
which are used to create individual instances of objects.
• A class is an abstract blueprint that creates more specific,
concrete objects.
• An object can be defined as a data field that has unique
attributes and behavior of a class.
• Classes contain functions called methods that are available
only to objects of that type. These functions are defined
within the class and perform some action helpful to that
specific object type.
File Handling
Basics of File Handling
• The term File Handling refers to the various operations like
creating the file, reading from the file, writing to the file,
appending the file, etc.
• There are two basic operation which is mostly used in file
handling is reading and writing of the file.
• The file becomes stream when we open the file for writing
and reading. A stream is a sequence of bytes which is used
for communication.
• Two stream can be formed from file
• input stream which is used to read the file
• output stream is used to write in the file
• In C#, System.IO namespace contains classes which handle
input and output streams and provide information about file
and directory structure.
Basics of File Handling(cont..)
File-Handling-Class-Hierarchy
Basics of File Handling(cont..)
The System.IO Namespace has various classes and types that allow
reading of files and writing to files. Some of the classes in this
namespace are given as follows:
Classes Description
FileStream This class provides a Stream for a file, supporting both
synchronous and asynchronous read and write operations.
StreamReader This class implements a TextReader that reads characters
from a byte stream in a particular encoding.
StreamWriter This class implements a TextWriter for writing characters to a
stream in a particular encoding.
This class represents a reader that can read a sequential series
TextReader
of characters.
TextWriter This class represents a writer that can write a sequential
series of characters. This class is abstract.
FileStream Class
• FileStream class is used to read from and write to any location within a file.
• It supports both synchronous as well as asynchronous read and write operations.
‣ FileStream Class implementation source code
using System;
using System.IO;
namespace FileHandling
{
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream fi = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);
fi.WriteByte(12);
fi.WriteByte(6);
fi.WriteByte(30);
fi.Close();
FileStream Class(cont..)
FileStream fo = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);
int i = 0;
Console.WriteLine("The contents of the file are:");
while ((i = fo.ReadByte()) != -1)
{
Console.WriteLine(i);
}
fo.Close();
}
}
StreamWriter Class
• The StreamWriter class implements TextWriter for writing character
to stream in a particular format.
• Some of the methods used by StreamWriter class are:
Method Description
Clears all the data from the buffer and write it in the stream
Flush()
associate with it.
using System;
using System.IO;
namespace FileHandling
{
public class StreamWriterExample
{
public static void Main(string[] args)
{
FileStream fs = new FileStream("e:\\file.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("File Handling in C#");
sw.Close();
fs.Close();
}
}
}
StreamReader Class
• The StreamReader class implements TextReader for reading
character from the stream in a particular format.
• The class contains the following method which are mostly used.
Method Description
Peek() Returns the next available character but does not consume it.
ReadLin Reads a line from the input stream and return the data in form
e() of string
• Note that all those classes are sealed class, so we cannot inherit
from it.
.NET Framework Data Provider for Oracle
}
}
catch (Exception e)
{
MessageBox.Show(ex.ToString());
}
finally
{
con.Close();
}
ADO.NET DataAdapter Class