2

I am stuck in implementation of generic class and interface. I am not sure what i wanted to do is possible or not.

here is my code:- I have defined a generic class whose type not define like

public class Response<T>
{

    public T Data { get; set; }

    public Response()
    {
        Data = default(T);
    }
}

and a Interface which right now have one function

public interface  IOInterface<T> where T : Response<T>
{
    Response<T> ReadAdvantechChannel(Dictionary<string, string> IOParameters);
}

And one derive class who will implements this interface :-

public class AdvantechOperation : IOInterface<T> 
{
    public Response<AIRecordInfo> ReadAdvantechChannel(Dictionary<string, string> IOParameters)
    {
        Response<AIRecordInfo> resp = new Response<AIRecordInfo>();
    }
}

and AIrecordinfo is again class

public class AIRecordInfo
{
    double[] ArryMaxValueAIdouble;
    double[] ArryMinValueAIdouble;
    double[] ArryAVGValueAIdouble;
}

what i want to do that implement a class who implement define interface but return type of interface function is not clear for my future derived class thats why i have made it generic and try to implement it my present class "AdvantechOperation". My interface will have three functions and all function return type will different to each other thats why i made it generic return type Response and in my present class AdvantechOperation, T replaced by AIRecordInfo. But I am facing below Error while compiling this project

Error 7 The type 'T' cannot be used as type parameter 'T' in the generic type or method 'IOModule.IOInterface'. There is no boxing conversion or type parameter conversion from 'T' to 'IOModule.Response'. D:\MTS\SMS\DAQAdvantechdll\DAQAdvantechdll\IOCommunication.cs 28 18 IOModule Error 8 'IOModule.AdvantechOperation' does not implement interface member 'IOModule.IOInterface.ReadAdvantechChannel(System.Collections.Generic.Dictionary)'. 'IOModule.AdvantechOperation.ReadAdvantechChannel(System.Collections.Generic.Dictionary)' cannot implement 'IOModule.IOInterface.ReadAdvantechChannel(System.Collections.Generic.Dictionary)' because it does not have the matching return type of 'IOModule.Response'. D:\MTS\SMS\DAQAdvantechdll\DAQAdvantechdll\IOCommunication.cs 28 18 IOModule

smn.tino
  • 2,272
  • 4
  • 32
  • 41
Shanu Mehta
  • 182
  • 1
  • 14

2 Answers2

1

Get rid of the generic type constraint for your interface and use AIRecordInfo as the generic type argument when implementing the interface:

public class AdvantechOperation : IOInterface<AIRecordInfo>
{ ... } 
Moho
  • 15,457
  • 1
  • 30
  • 31
1

It seems to me that you want this:

public class Response<T> { }

public interface IOInterface<T> where T : Response<T>
{
    T ReadAdvantechChannel(Dictionary<string, string> IOParameters);
}

public class AdvantechOperation : IOInterface<AIRecordInfo>
{
    public AIRecordInfo ReadAdvantechChannel(Dictionary<string, string> IOParameters)
    {
        return new AIRecordInfo();
    }
}

public class AIRecordInfo : Response<AIRecordInfo>
{
    double[] ArryMaxValueAIdouble;
    double[] ArryMinValueAIdouble;
    double[] ArryAVGValueAIdouble;
}

Response<T> no longer needs a body because IOInterface<T>'s method simply returns T which must inherit from Response<T>.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • why is `AIRecordInfo` deriving from `Response`? – Moho Sep 26 '17 at 11:18
  • @Fildor - No, this is perfectly legal - it doesn't cause a stack overflow as it isn't executing on the stack. It's just a single recursive reference. It's like drawing a circle doesn't magic suck all the ink from your pen. – Enigmativity Sep 26 '17 at 11:28
  • @Moho - `AIRecordInfo` needs to derive from `Response` to meet what the OP wants to do. It's perfectly legal code. – Enigmativity Sep 26 '17 at 11:29
  • Ah, dough! Lack of coffee. `default(T)` will be `null` for Ref-Types ... – Fildor Sep 26 '17 at 11:33
  • OP's design is flawed, hence the question; your implementation, while compiling, does not correct one of the flaws; `AIRecordInfo` is meant to be the `Response`'s data, not the response itself with possible infinitely nested `AIRecordInfo` – Moho Sep 26 '17 at 11:39
  • @Moho While that may be true, that wasn't what the question was about. So I consider this a valid answer. It could as well be that what you consider a "flaw" is absolutely wanted by OP. – Fildor Sep 26 '17 at 11:49
  • @Moho - Yes, you're right. This probably means the OP can get rid of the guts of `Reponse` and just make it a empty type. – Enigmativity Sep 26 '17 at 12:10
  • @Enigmativity - `AIRecordInfo.Data` property is of type `AIRecordInfo`, which is a `Response` in your implementation, which therefore has a `Data` property of type `AIRecordInfo`/`Response`, etc., so it can have infinite nested `Data` properties – Moho Sep 26 '17 at 12:10
  • @Moho - Yes, sorry, I deleted my previous comment before you posted, but obviously I wasn't quick enough. I thought you were referring to the nested type definition and not the nested `Data` property. My solution removes the need for it. – Enigmativity Sep 26 '17 at 12:16
  • @Enigmativity if I will define class AdvantechOperation with implementing AIRecordInfo type interface. then for all function in IOInterface must return type of AIRecord info. Right now in interface only one function available but i will add more function with Genric return type. – Shanu Mehta Sep 27 '17 at 04:15
  • @PriyankaMehta - No, you can do `class AdvantechOperation : IOInterface, IOInterface, IOInterface`, etc. – Enigmativity Sep 27 '17 at 05:27