Generic interfaces
Asked Answered
B

3

6

Here is my code

public interface ITranslator<E, R>
{       
    E ToEntity<T>(R record);
}

class Gens : ITranslator<string, int>
{
    #region ITranslator<string,int> Members

    public string ToEntity<MyOtherClass>(int record)
    {
        return record.ToString();
    }

    #endregion
}

When I compile this, I get an error Type parameter declaration must be an identifier not a type

Why is that I cannot have ToEntity<MyOtherClass> but can only have ToEntity<T> ??

Edit: what is MyOtherClass doing ? I am converting between entities(POCOs equivalent of Entity framework) and record(Object returned by the framework) for multiple tables/classes. So I would want to use this to do my class specific conversion

Bedfast answered 29/1, 2010 at 23:38 Comment(1)
@Ram, you need to tell us: how is MyOtherClass supposed to be involved in your method Gens.ToEntity? From the code it's not clear at all. If we knew your intention behind it, we could probably offer some more useful feedback.Sweater
S
9

Your interface has a generic ToEntity<T> method that you've made non-generic in your implementation class Gens as ToEntity<MyOtherClass>. (A generic method could take any type parameter, possibly given certain constraints on T. Your Gens class is trying to provide a definition for ToEntity only for the type parameter MyOtherClass, which defeats the purpose of generics.)

In your code example, it's unclear how your Gens class is trying to use the MyOtherClass type; it's certainly not involved in the logic of ToEntity. We'd need more information to be able to guide you further.

To illustrate, here's what your current definition of the ITranslator<E, R> interface offers, in plain English:

"I provide a mechanism to translate any record of type R into an entity of type E, this mechanism being dependent upon any user-specified type T."

Your Gens class, on the other hand, the way it's currently designed, "implements" the above interface like so:

"I can translate integers to strings. I provide the illusion of allowing the user to specify a type to control how this translation is performed, but in fact there is no choice of type. The MyOtherClass class is involved somehow; that's all I can say."

From these two descriptions, it's clear that the Gens class is not really doing what the ITranslator<E, R> interface guarantees. Namely, it is not willing to accept a user-specified type for its ToEntity method. That's why this code won't compile for you.

Sweater answered 29/1, 2010 at 23:43 Comment(1)
very nice lucid answer. The class does not implement the interface in its true spirit. Hence the errorBedfast
S
2

You must declare a constraint on a generic type.

public string ToEntity<T>(int record) where T : MyOtherClass
Sixteenmo answered 29/1, 2010 at 23:43 Comment(0)
M
1

That compiles OK for me in LINQpad. Maybe you have a type named E, R, or T somewhere?

Ahh I see what you're trying to do... you have MyOtherClass defined as a class somewhere yet you're trying to use it as a type argument in ToEntity. How exactly do you want MyOtherClass involved in ToEntity?

Monomorphic answered 29/1, 2010 at 23:42 Comment(1)
That code by itself that you pasted is actually valid code and compiles fine. I'm not quite sure what it is you're intending to do with MyOtherClass in your example.Monomorphic

© 2022 - 2024 — McMap. All rights reserved.