CHPTR 3 ADT-II
CHPTR 3 ADT-II
Subject Instructor
Prof. J. T. Patil
Assistant Professor, Information Technology Department, JJMCOE, Jaysingpur
Structure overview
Class T.Y.Btech Semester- VI
Credits 03
C# fundamentals:
Data types - Value types, Reference types, boxing and unboxing, Arrays, Pass by value and
Unit 2 by reference and out parameters, params parameter. Namespaces, classes, objects, structs: 3 Hr
definition and creation.
File handling:
The abstract stream class, working with StreamWriters and StreamReaders, Working with
Unit 4 4 Hr
StringWriters and StringReaders, Working with BinaryWriters and BinaryReaders.
ADO.NET:
Unit 5 Exploring ADO.net Entity framework, Connected and disconnected architecture, data 4 Hr
access with ADO.net.
Reference Books:
1. Microsoft Visual C# 2010 Step by Step: John sharp, Microsoft Press
2. NET 4.5 Programming (6 – in -1) Black Book – Kogent – Dreamtech
Press
3. CLR via C# :Jeffrey Richter, Microsoft Press, 3rd edition
4. ASP.Net 4.5 Black Book ,Dreamtech ,Wiley International.
Unit 1 : Delegates and Events
Creating and using delegates
multicasting with delegates
event sources, event handlers
GUI Programming: Introduction to GUI Application and
their components,
Windows forms – buttons, check boxes, radio buttons, panels,
group boxes, list boxes, picture boxes, Menus, ToolStrips,
StatusStrips and progress bars, events,
Creating and using MDI application.
Delegates:
It is a object reference
Here,
delegate - a keyword
For example,
using System;
using System.Collections.Generic;
class Program
{
// define a method that returns sum of two int numbers
static int calculateSum(int x, int y)
{
return x + y;
}
// define a delegate
public delegate int myDelegate(int num1, int num2);
static void Main()
{
// create an instance of delegate by passing method name
myDelegate d = new myDelegate(calculateSum);
// calling calculateSum() using delegate
int result = d(5, 6);
Uses:
more powerful
safe to use(type-safe)
promote reusability of code and implement flexibility
notify which method to call when an event is triggered
define callback methods
Delegates work with methods
properties of delegates
1. type /signature of method that delegate point to.
2. delegate reference can be used to reference to a method.
3. actual method referenced by the delegate
Type /signature of method that delegate point to.
SIGNATURE:
INT MYMETHOD (INT V , INT D)
namespace ConsoleApplication1
{
class Program
{
delegate int myd(int p, int q);
{
return a+b;
}}}
Multicast delegate
// multicast delegate
// calls difference() method
d += obj.difference;
Events are user actions such as key press, clicks, mouse movements, etc., or
some occurrence such as system generated notifications.
Events allow you to tie your own code into the functioning of an
independently created component
The user does something (clicks a button, moves a mouse, changes a value, etc.)
and the program reacts in response
Time-based events
Multicast delegates allow multiple objects to register with the same event
Syntax:
MyDelegate a;// delegate
event MyDelegate b; //event
class MyClass
{
public event MyDelegate GoodNews
{ add
{
// add accessor code here
} }
It is very easy and at the end of this point, you will understand how
to create events and how to associate an event with a delegate, how
to raise an event, and how to handle an event in C# with examples.
In C#, it’s very simple to define an event.
So, the event can be defined inside a class using the event keyword and the syntax
is given below.
First, we need to define one delegate, and using that delegate only we need to
define an event as shown in the below image.
As you can see, events are created using the delegate. Here, we created the
WorkPerformed event using the WorkPerformedHandler delegate.
This is important because an event on its own does not really do anything. You
must have a pipeline or delegate which routes the data from Point A to Point B, that
is from Event Raiser to Event Handler or Event Listener and that is going to be our
delegate. So, events are really wrappers around the delegate. This is the entire
Example to understand How to Create Custom Event in C#:
1. using System;
2. namespace DelegatesDemo 12. public void DoWork(int hours,
WorkType workType)
3. { 13. {
4. //Step1: Define one delegate 14. //Raising Events
5. public delegate void WorkPerformedHandler(int hours, 15. }
16. }
WorkType workType);
17. public enum WorkType
6. public class Worker 18. {
7. { 19. Golf,
20. GotoMeetings,
8. //Step2: Define one event to notify the work progress
21. GenerateReports
using the custom delegate
22. }
9. public event WorkPerformedHandler WorkPerformed; 23. }
10. //Step2: Define another event to notify when the work is
completed using buil-in EventHandler delegate
11. public event EventHandler WorkCompleted;
Raising Events in C#:
Once we defined the Events, then we need a way to Raise the Events. And this is important
otherwise, the listener would have no way to get the event data. Now, we are going to discuss
the different processes or techniques of Raising Events in C#.
Events are Raised simply by calling the events like a method. So, you just need to use the
event name and you need to pass the parameter values. Why? Because behind the event, we
have delegates only. So, the way we invoke a delegate, in the same way, we need to invoke an
event or you can raise an event in C#.
Handling Events in C#:
Now, we are going to discuss how to handle the event in C# which is raised by
the Event Raiser.
Event handler cannot return a value.
The client object can act on an event by adding an event handler to the
event.
Instantiating Delegates and Handling Events in C#:
We have already discussed that the delegate method signature and event handler method
signature should and must be the same otherwise the event handler method will not get the
data from the delegate or pipeline. If the handler method wants to receive the data from the
pipeline, then the handler must have the same number, type, and order of parameters as the
delegate. For a better understanding, please have a look at the below image.
In .NET Framework, we can use the += operator to attach an event with an
event handler. Here, is an example.
Difference between delegates and events
1. An event is an event.
4. Delegate ( as its English meaning ) : Secretary or someone who connect two persons
through him .
6. But Event fire more than one delegate , each call one method
8. Event is a Modifier
9. Events enable a class or object to notify other classes or objects when something of
interest occurs
Steps for the events
The event keyword lets you specify a delegate that will be called upon the occurrence of some
"event" in your code.
The delegate can have one or more associated methods that will be called when your code
indicates that the event has occurred The following steps must be taken in order to create and use
C# events:
1. Create or identify a delegate. If you are defining your own event, you must also ensure that
there is a delegate to use with the event keyword.
B.(optional) A method that verifies that an instance of the delegate declared with the event keyword
exists.
C.Methods that call the event. These methods can be overrides of some base class functionality.
3. Define one or more classes that connect methods to the event. Each of these
classes will include:
Associate one or more methods, using the += and -= operators, with the
event in the base class.
The definition of the method(s) that will be associated with the event.
GUI stands for Graphical User Interface. It refers to an interface that allows one to interact with
electronic devices like computers and tablets through graphic elements. It uses icons, menus and other
graphical representations to display information, as opposed to text-based commands. The graphic
elements enable users to give commands to the computer and select functions by using mouse or other
input devices.
With advancing technology, the programmers and designers create complex GUI that work with more
efficiency, accuracy and speed
History of GUI
Earlier, there was no GUI so people used to interact with the command-line interface(CLI).
The CLI was not that friendly to use and the end-user was not familiar with all the
commands. So to bridge this gap, GUI was introduced. The main aim of the GUI was to
make the applications much more user-friendly.
GUI Components and Advantages
GUI provides a set of components to interact with software and Hardware
A GUI consists of graphical elements such as cursors, icons, and buttons that are
enhanced with sound and visual effects. Through these objects, users can use a computer
without knowing the commands.
The graphical User Interface is visually very appealing and detailed oriented.
It ensures that people with little or even no knowledge of computers can use it and perform basic
computer functions.
Graphical User Interface is easy to use since it does not require the user to use any command.
Through Graphical User Interface, each and every response from the computer is visually
communicated.
It makes the entire design more attractive and allows developers to have more control over visual
customization to improve user experience.
Users can easily and quickly finish tasks using Graphical User Interface, unlike in CUI, which
requires multiple typed commands to complete a task.
Thank You!