help! about delegate problem

  • Thread starter Thread starter Jet Leung
  • Start date Start date
J

Jet Leung

Hi all,
I want use delegate in the program,but I don't know how is it work. How can
I do that? What is defferent between use pure function and use delegate to
handler the function?
My code:
class CXmlHandler
{
delegate string deleXmlHandler(string A);
string MultiText;
[STAThread]
static void Main(string[] args)
{
string XmlFilePath=@"g:\\Folders.xml";
CXmlHandler x_Handler=new CXmlHandler();
// It can show in the Console when not use delegate
//Console.WriteLine(x_Handler.GetXmlHandler(XmlFilePath));

// When I use this delegate to handler my function, it
// can't show the result what I need
x_Handler.ExcuteDelegateGetXmlHandler(XmlFilePath);

}
private void ExcuteDelegateGetXmlHandler(string Args)
{
deleXmlHandler D_getxmlhandler=new deleXmlHandler(this.GetXmlHandler);
D_getxmlhandler.BeginInvoke(Args,null,null);
Console.WriteLine("press Enter to return......");
Console.ReadLine();
}
private string GetXmlHandler(string XmlFilePath)
{
XmlReader xmlR=new XmlTextReader(XmlFilePath);
while(xmlR.Read())
{
int AttribCount=xmlR.AttributeCount;
int i;
if(xmlR.HasAttributes)
{
for(i=0;i<AttribCount;i++)
{
string AttribText;
AttribText=xmlR.GetAttribute(i).ToString();
MultiText+=AttribText + "\n";
}
}

}
return MultiText;
}
 
Jet,

Delegates are a way of specifying a contract in an abstract manner
(somewhat similar to interfaces, except that they define the contract on the
type level). It basically says "I don't care what the implementation is,
but I want the signature of the method to match this".

Why would you want this? Well, event notification is a really good
example, because without delegates, the object firing the event would have
knowledge of the type that you want to perform the callback on. This
requirement would destroy any kind of reusable design.

Hope this helps.
 
Back
Top