DELEGATION PROBLEM

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();
 
S

Savas Ates

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
?
 
B

Barry Kelly

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
 
S

Savas Ates

thank you sir.. Can you offer me any link to have more info about delegation
?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top