Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generic Delegates 1

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
My current code is like this;
Code:
class foo
{
public delegate object delegateMethodType(string s);

public delegateMethodType MethodToExecute;

public object DoWork(string s)
{

    MethodToExecute(s);
    ...
    ...
}
}

class bar
{
    foo f = new foo();
    f.MethodToExecute = SomeMethod;

    MyClass o = (MyClass)f.DoSomeWork("data");
}

This is so I can use the bar class in a number of different places in my code, and get my results back depending on the delegate I pass in.

All Straight forwards so far.

However, I am having to do a lot of Handling of object casting to and from different classes.

This set me to thinking about using Generics. Logically, I was expecting to be able to use something like;

Code:
class foo
{
public delegate T delegateMethodType<T>(string s);

public delegateMethodType MethodToExecute;

public T DoWork<T>(string s)
{

    MethodToExecute(s);
    ...
    ...
}
}

class bar
{
    foo f = new foo();
    f.MethodToExecute = SomeMethod;

    MyClass o = f.DoSomeWork<MyClass>("data");
}

However, when I try to build code like the above, I get an error on the line where I declare the delegate within class foo. (i.e. public delegateMethodType MethodToExecute)

So, the 6 million dollar question is, how do I represent a generic method in such a way?

(If it helps the error message I have is using the Generic Type ... reguires 1 Type arguments )

Thanks

K
 
I think I understand what you are trying to achieve. I do not think i deserve the 6 million dollars but anyway here are some comments:


The error you are getting is related to the fact that you are trying to define a variable of a generic type without specifying a specific type. As i understand it, it is OK to define classes and delegates, procedures and other types with a generic type placeholder but it is NOT OK to define true variables without specifying a real (specific) type to take the place of the placeholder.

So :

Code:
public delegateMethodType MethodToExecute; //not OK 
public delegateMethodType<int> MethodToExecute; //OK

Of course the second definition is OK for the compiler but certainly not for you as you appear to require the delegateMethodType to be returning a generic type.


So what can you do to satisfy both the compiler and you? Well here is my suggestion:

1. Do not define a variable of generic type placeholder without specifying the real type placeholder
(this will keep the compiler happy)

2. Add one more parameter of your delegate to your DoWork procedure
(this may or may not make you happy)



Here is an example which will certainly keep the compiler happy :)

Code:
    public class foo
    {
        public delegate T delegateMethodType<T>(string s);

        //sorry can not have this
        //public delegateMethodType MethodToExecute;  

        //You can have this if you want 
        public T DoWork<T>(delegateMethodType<T> MethodToExecute, string s)
        {

            return MethodToExecute(s); // MethodToExecute(s);

        }       
    }

    class bar
    {
	//a little test
        void Test()
        {
       	    foo f = new foo();
	    
            foo.delegateMethodType<int> aMethod = SomeMethod;
            
            foo.delegateMethodType<foo> aFooMethod = SomeFooMethod;

            foo.delegateMethodType<bar> aBarMethod = SomeBarMethod;

            int o = f.DoWork<int>(aMethod,"data");

            foo aFoo = f.DoWork<foo>(aFooMethod, "data");

            bar aBar = f.DoWork<bar>(aBarMethod, "data");

        }

        int SomeMethod(string s)
        {
            return 0;
        }

        foo SomeFooMethod(string s)
        {
            return null;
        }

        bar SomeBarMethod(string s)
        {
            return null;
        }
    }

Hope this helps a little.






"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Guess what !! This works too.

Code:
    public class foo
    {
        public delegate T delegateMethodType<T>(string s);

        //sorry can not have this
        //public delegateMethodType MethodToExecute;  

        //You can have this if you want 
        public T DoWork<T>(delegateMethodType<T> MethodToExecute, string s)
        {

            return MethodToExecute(s); // MethodToExecute(s);

        }       
    }

    class bar
    {
	//a little test
        void Test()
        {
       	    foo f = new foo();
	    
            int o = f.DoWork<int>(SomeMethod, "data");

            foo aFoo = f.DoWork<foo>(SomeFooMethod, "data");

            bar aBar = f.DoWork<bar>(SomeBarMethod, "data");;

        }

        int SomeMethod(string s)
        {
            return 0;
        }

        foo SomeFooMethod(string s)
        {
            return null;
        }

        bar SomeBarMethod(string s)
        {
            return null;
        }
    }

Hope this helps a little more :)





"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top