D
damiensawyer
Hi,
Can someone please explain to me something about delegates?
My understanding is as follows. A delegate is basically an object that
can hold a reference to a "method" somewhere. That is, it's
essentially a pointer to a piece of code somewhere else in memory.
It therefore (to me anyway) makes sense to define delegates with their
signatures and be able to use those signatures at different points in
code. That way, if the signatures of various methods are defined by
the delegate, you 'know' that you can store references to those
methods in your delegates.
My question is, is there a way to define a method, referring to a
delegate to source it's parameter list?
I hope this makes sense. Please feel free to pull me up if I'm missing
the point completely.
Thanks in advance.
Damien
--------- working sample --------
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public delegate void WriteLogHandler(WriteLogEventHandler
args); // note - deliberately left out "object Sender" parameter to
keep demo simple.
public class WriteLogEventHandler : EventArgs
{
private string message;
public WriteLogEventHandler(string Message)
{
message = Message;
}
public string Message
{
get{return message;}
}
}
public class Person
{
public event WriteLogHandler WriteLog;
public void FireAnEvent()
{
if (WriteLog != null) WriteLog(new
WriteLogEventHandler("Hello Delegate World"));
}
}
protected void Page_Load(object sender, EventArgs e)
{
Person SamplePerson = new Person();
SamplePerson.WriteLog += new
WriteLogHandler(SamplePerson_WriteLog);
SamplePerson.FireAnEvent();
}
/// The question I have is, can I pass in some reference to
"WriteLogHandler"
/// as the argument to the below method? By doing it the way it is
below, I have to keep the delegate and
/// this signiture (and however many others) in sync.
void SamplePerson_WriteLog(WriteLogEventHandler args)
{
this.Form.InnerHtml += args.Message + "<br/>";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Delegate Primer</h3>
</div>
</form>
</body>
</html>
Can someone please explain to me something about delegates?
My understanding is as follows. A delegate is basically an object that
can hold a reference to a "method" somewhere. That is, it's
essentially a pointer to a piece of code somewhere else in memory.
It therefore (to me anyway) makes sense to define delegates with their
signatures and be able to use those signatures at different points in
code. That way, if the signatures of various methods are defined by
the delegate, you 'know' that you can store references to those
methods in your delegates.
My question is, is there a way to define a method, referring to a
delegate to source it's parameter list?
I hope this makes sense. Please feel free to pull me up if I'm missing
the point completely.
Thanks in advance.
Damien
--------- working sample --------
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public delegate void WriteLogHandler(WriteLogEventHandler
args); // note - deliberately left out "object Sender" parameter to
keep demo simple.
public class WriteLogEventHandler : EventArgs
{
private string message;
public WriteLogEventHandler(string Message)
{
message = Message;
}
public string Message
{
get{return message;}
}
}
public class Person
{
public event WriteLogHandler WriteLog;
public void FireAnEvent()
{
if (WriteLog != null) WriteLog(new
WriteLogEventHandler("Hello Delegate World"));
}
}
protected void Page_Load(object sender, EventArgs e)
{
Person SamplePerson = new Person();
SamplePerson.WriteLog += new
WriteLogHandler(SamplePerson_WriteLog);
SamplePerson.FireAnEvent();
}
/// The question I have is, can I pass in some reference to
"WriteLogHandler"
/// as the argument to the below method? By doing it the way it is
below, I have to keep the delegate and
/// this signiture (and however many others) in sync.
void SamplePerson_WriteLog(WriteLogEventHandler args)
{
this.Form.InnerHtml += args.Message + "<br/>";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Delegate Primer</h3>
</div>
</form>
</body>
</html>