DELEGATION PROBLEM

  • Thread starter Thread starter Savas Ates
  • Start date Start date
S

Savas Ates

I want create a delegate method like that . However It returns ..

CsharpWeb1.deneme.test()' does not match delegate 'void
CsharpWeb1.deneme.foo()
Method 'CsharpWeb1.deneme.test2()' does not match delegate 'void
CsharpWeb1.deneme.foo()'

What is the my mistake ?

public class deneme

{


public int sal ()

{

foo x=new deneme.foo(test);

x+=new deneme.foo(test2);

}


public delegate void foo();

public int test ()

{


return 10;

}

public int test2()

{

return 100;

}

in Page_load

deneme sailim =new deneme ();

sa2.Text=sailim.sal().ToString();
 
ok thanks for your helping

I changed the code like that

public class deneme

{


public delegate int foo();

public foo sal ()

{

foo x=new deneme.foo(test);

x+=new deneme.foo(test2);

return x;



}

public int test ()

{


return 10;

}




public int test2()

{

return 100;

}




}

It returns
CsharpWeb1.deneme+foo
I expect 10 and 100 which test and test2 methods return ? hoe can i get it
?
 
Savas Ates said:
foo x=new deneme.foo(test);

x+=new deneme.foo(test2);

return x;
It returns
CsharpWeb1.deneme+foo
I expect 10 and 100 which test and test2 methods return ? hoe can i get it

The type of x is foo. Because it's inside the deneme class, and the
deneme class is inside CsharpWeb1, and the default ToString() of
System.Object is to return the name of the type, and .NET creates nested
class names with '+', the total name of the *type* of x is
"CsharpWeb1.deneme+foo".

To call it, you need to use '()':

return x();

This will return the value of the last delegate attached, i.e. the value
of test2(). The value of the test() call is lost. For this reason, it's
recommended that delegates which return values shouldn't be combined.

-- Barry
 
Back
Top