0% found this document useful (0 votes)
128 views45 pages

Delegates

Delegates in C# allow a programmer to encapsulate a reference to a method inside a delegate object. A delegate object can then be passed to code which can call the referenced method without knowing at compile time which method will be invoked. Delegates make methods anonymous and allow methods to be passed around as parameters. There are two stages to using delegates - defining the delegate type, and then instantiating delegate objects of that type to reference methods.

Uploaded by

Deepank Agrawal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views45 pages

Delegates

Delegates in C# allow a programmer to encapsulate a reference to a method inside a delegate object. A delegate object can then be passed to code which can call the referenced method without knowing at compile time which method will be invoked. Delegates make methods anonymous and allow methods to be passed around as parameters. There are two stages to using delegates - defining the delegate type, and then instantiating delegate objects of that type to reference methods.

Uploaded by

Deepank Agrawal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

Delegates in C#

Delegates can best be seen as a new type of


object in C# - with some similarities to classes.

A delegate in C# is similar to a function pointer


in C or C+ .A delegate allows a programmer to
encapsulate a reference to a method inside a
delegate object.The method can be either a
static or an instance method.A delegate object
can then be passed to code,which can call the
referenced method,without having to know at
compile time which method will be invoked.
A difference between delegates in C# and
function pointers in C++ is that delegates are
object-oriented and secure.
An interesting and useful property of a
delegate is that it does not know or care
about the class of the object that it
references.Any object will do;all that
matters is that the method’s argument
types and return type match those of the
delegate.
When we want to use a class in C#,there are two
stages.First,we need to define the class – that is
we need to tell the compiler what fields and
methods make up the class.Then we instantiate
the class – we create an object of that class.
With delegates it’s the same thing.We have to
start off by defining the delegates we want to
use. In the case of delegates,defining it means
telling the compiler what kind of delegates of
that type will represent.Then we have to create
one or more instances of that delegate.
The syntax of defining delegates looks like this:
Delegate void VoidOperation(int x);

In this case we have defined a delegate


called VoidOperation and we have
indicated that each instance of this delegate can
hold details of a method that takes one int
parameter and returns void.

One good way of understanding delegates is by


thinking of a delegate as something that gives a
name to a method signature.
Depending on how visible we want our definition to be we
can apply any of the Normal access modifiers to delegate
definitions-
public,private,protected

Delegates are implemented as instances of classes


derived from the base class,System.Delegate.
The C# compiler is aware of thisclass,and uses its
delegate syntax to shield us from the details of the
opertaion of this class.
With classes there are two distinct terms –
‘class’ which indicates the border
definition;and ‘object’ which means an
instance of the class but with delegates
there is only one term .When you create
an instance of delegates what you have
created is also referred to as a delegate.
using System;
delegate void SimpleDelegate();
class Test1
{
static void F() {
System.Console.WriteLine("Test1.F");
}
static void Main() {
SimpleDelegate d = new SimpleDelegate(F);
d();
}
}

Program output is:


Test1.F
There is not much point in instantiating a delegate
for a method and then immediately calling it via
the delegate, as it would be simpler to call the
method directly.Delegates really show their
usefulness when their anonymity is used. The
example
void MultiCall(SimpleDelegate d, int count) {
for (int i = 0; i < count; i++)
d();
}
}
shows a MultiCall method that repeatedly calls a
SimpleDelegate. The MultiCall method doesn’t
know or care about the type of target method for
the SimpleDelegate, what accessibility the method
has, or whether or not the method is static.
One feature of delegates is that they are type-
safe to the extent that they ensure that the
signature of the method being called is correct.
However they do not care what type of object
the method is being called against,or even
whether the method is static or an instance
method.
Custom Dialog

• Displays a message in a dialog box, waits for


the user to click a button, and then returns an
integer indicating which button the user clicked.
Custom Dialog
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Do you want to continue?"
style = MsgBoxStyle.YesNo
title = "MsgBox Demonstration"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then
Else
End If
Server Control and validator
• When we design the user interface of a web
Page by using controls called ASP.NET server
Controls.server control are different from usual
Windows controls because they work with the
Asp.net framework.validator is done when you
Perform event handling on button or textbox etc.
Server Controls

• Important Server Control are


1.Button
2.Checkbox
3.Label
4.Data Grid
5.Textbox
Etc….
<script language=“c#” runat=“server”>

Sub page_add()
Response.Write(“perform event handling”);
End Sub
</script>

<asp:textbox id=“txt” runat=“server”/>


<asp:button id=“txt” onclick=“add”
runat=“server”/>
Sorting And Filtering
• Sometimes,when we need not display all the
Retrieved records.for example,we may need to
display only the details of customers living in
California.In such cases,we apply the filter the
Data so as to display only the desired records.In
Addition,we can sort the data to display it in
Ascending or descending order of a particular
Column.
There are two Methods for displaying
filtered data.
• 1.Creating a Parameterized querires

• 2. Filtering a dataset

• Ex. Select id,name,state


from auther
Where state=“ “
Filtering a Dataset
• After a data is retrieved in a dataset,we can
Filter the data stored in the dataset to view
Customized records.
1.Select method()
We can use the select method of the DataTable
Class to filter the data,the order of the records
In the table does not change based on the
Parameter passed.Only the data is presented
In a filtered format
The code given below illustrates the use of
the select() method to filter data and display
the filtered data in a ListBox control when
the Button is Clicked.
• Dim dt As DataTable
• dt = DataSet11.Tables("emp")
• Dim b As String
• Dim c As String
• b = "sal >4000"
• c = "EMPNO"
• Dim result() As DataRow
• result = dt.Select(b, c)
• Dim ctr As Integer
• For ctr = 0 To (result.Length - 1)
• ListBox1.Items.Add(result(ctr)
("ENAME").ToString)
• Next
Xml Data Management
• In the business world,trading partners share
Data to carry out business.The data that trading
Partners share can be stored in different data
Formats.To seamlessly integrate and exchange
The data stored in different data formats,we
Need to convert the data into one common
format .XML as the common data format for
Sharing and exchanging data between different
Application.
Xml Data Management
• XML is a cross-platform, and hardware and
Software independent,text-based markup
Language that enables you to store data in a
Structured format by using meaning tags.XML
Stores structured data in XML documents.XML
Documents are similar to databases.However,
Unlike databases,XML documents store data in
the form of plain text,which can be used
Across platforms.
Support for XML in ASP.NET
• .NET Framework has extensive support for
Working with XML documents.In .NET Framework
,the support for XML documents includes:
1)XML Namespace

2)XML designer

3)XML Web Server Control

4) XML DOM support


XML Web Server Control has the following propertis.

• 1.Document Source: This Property you to


specify the URL or Path of the XML document
To be displayed in the web Form.

2.TransformSource: This property allows you to


Specify the URL of the XSLT file,which transforms
The XML document into the required format
before it is displayed in the web from.
Reflection or Introspection

• Reflection or Introspection is when you look


Within to find out about yourself.In the same
Way we need a method by means of which,our
Program can find out all about a class.We need
To know how many methods,properties etc.
While our program is executing or running.This
distinction is important and we could always
read the documentation if we wanted to know
More about the functionality of a class.
Namespace for Reflection

• Using System;
• Using System.Reflection
The Reflection namespace contains classes that provide
a managed view of loaded types, methods, and fields,
with the ability to dynamically create and invoke
types.
• Classes for Reflection

a)Type from System Namespace

b)MemberInfo from System.Reflection


Reflection(To know about class)

• Typeof is a Keyword,it needs a class name


As a parameter.

Impoertant metods for Type class are


1.Name
2.FullName
Note:- int is an alias for a structure Int32 in
The System namespace
Example.
• Using System;
Class supplier
{
Public static void Main()
{
Type m; m=typeof(int);
System.Console(m.Name+” ”+m.FullName);
}
}
Class item
{
}
• Class supplier
{
Public static void Main()
{
Type m=typeof(item);
MemberInfo[] n;
n=m.GetMembers();
Console.WriteLine(n(0).
}
Class item
{
Public void get()
{
}
}
The Six Validation Controls.
• Each of the Six Controls has specific capabilities
and functionality.Which one you elect to use
depends upon your needs.

• 1.compare Validator
Compares an input control to another input
Control.The Compare validator control is good for
Password fields.
RequiredFieldValidator

• 2.The RequiredFieldValidator control checks


the input to see if the user has entered or
select anything.This Control is perfect for
Forms that require data such as a password
And confirm password.
3.RangeValidator

• RangeValidator is similar to compareValidator,


But instead of just comparing one value against
another,RangeValidator can check that a value
exists between two other values.
4.RegularExpressionValidator

• RegularExpressionValidator checks the input


Against a regular expression.Regular expressions
are good for parsing string,this validator would
thus be good for validating against a regular
expressions to determine ZIP code validity and
Making sure password as aplhanumeric only.
5.CustomValidator

• The Customvalidator is a catch-all control that


Allows you to define your own validation needs.
6.Validationsummary: The alidationSummary
controls is different from the other controls.
Instead of providing validation directly,it contains
The error message from the other validation
control.This control can be used to provide a
list of errors at the bottom of the page.
Web Service
• A Web Service exposes a number of methods
That provide functionality that can be used by
One or more applications or have web methods
That provide functionality that can be used by
Web application like ASP.NET.The methods that
Provide such functionality are web methods.The
Functionality exposed by a Web Service can be
Accessed by applications by using Internet
standars,such as SOAP(Simple Object Access
Protocol).
Web Service
• Soap is a protocol that uses XML to describe
Data and HTTP to transmit application data.An
Application that uses a Web Service is called Web
Service Client.
Soap is able to communicate with other,a Web-
Service and a client application must agree upon
A common protocol.SOAP is a standard commni-
Cation protocol for interchanging information in a
Structured format in a distributed environment.
Web Service
• The information exchanged between the client
Application and the web service is called a
message.when a client application makes a
Request for a web method ,a soap packet is
created.This packet contains the name of the
Web method to be invoked and the parameters
To be passed to the web method in an xml
format.This information is used to invoke the
web method with the appropriate parameters.
When the SOAP packet arrives at the web server
Web Service
• On which the web services resides,the web
Method name and its parameters are extracted
from the SOAP packet and the apppriate web
method is invoked.

WSDL: To be able to use a Web Service,the


Developers of a client application need to know
The methods exposed by by the Web Service and
the parameters to be passed to these methods.
Web services Description Language(WSDL)
Web Service
• WSDL is a markup language that describes a
Web service.

UDDI:(Universal Description Discovery and


Integration )The Directory contains the Web
Services
And the WSDL document for the web service
WSDL
UDDI
Document

Locating a Describes
Web Service

Client
Application Web Service
Communication through SOAP

messages
HTTP Protocol
• HTTP-GET and HTTP-POST are standard
protocols that use HTTP (Hypertext Transfer
Protocol) verbs for the encoding and passing of
parameters as name/value pairs, along with the
associated request semantics.

• HTTP-GET passes its parameters in the form of


UUencoded text, which is appended to the URL
of the server handling the request. The
appended parameters are also referred to as a
query string
HTTP Protocol

• However, instead of being passed as part


of the URL, the name/value pairs are
passed inside the actual HTTP request
message
SOAP PROTOCOL
with Web Services
• SOAP (Simple Object Access Protocol ) is a
Simple lightweight XML-based protocol for
exchanging structured and type information on
the Web . The overall design goal of SOAP is to
keep it as simple as possible, and to provide a
minimum of functionality. The protocol defines
a messaging framework. As a result, the
protocol is highly modular and very extensible .
The SOAP protocol specification consists of four
main parts
Soap Protocol
• The first part defines a mandatory extensible
envelope for encapsulating data. The SOAP
envelope defines a SOAP message.

• The second part of the SOAP protocol


specification defines optional data encoding
rules for representing application-defined data
types and directed graphs
Soap Protocol
• The third part defines ( request/response)
message exchange pattern .

• The fourth part of the specification defines a


binding between SOAP and HTTP
Web Service
• A web service is similar to a component that
Provides a specific functionality.However
component

You might also like