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;
}
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;
}