c# - interface generic return type -


i have serveral classes similar method signatures wish capture in interface:

namespace mylib  public class clientlist     public icollection<client> fetch() {     {         //do stuff        return this.createcollection();     }      private icollection<client> createcollection()     {         list<client> clientlist = new list<client>();         // populate list         return clientlist;     }  public class productlist     public icollection<product> fetch() {         //do stuff        return this.createcollection();     }      private icollection<product> createcollection()     {         list<product> productlist = new list<product>();         // populate list         return productlist ;     } 

i interface method signature fetch returns icollection, type undefined (as different every list). ensure each *list object have fetch method , new ones won't have 'getlist' or other such named calls. after doing bit of research believe generics may way go, i'm unsure how.

i tried

public interface idatarequest     icollection<t> fetch<t>(); 

but when implemented as

public icollection<client> fetch<client>() 

i got error on 'return this.createcollection();':

cannot implicitly convert type 'system.collections.generic.icollection<mylib.client>' 'system.collections.generic.icollection<client>'. explicit conversion exists (are missing cast?)

i thought maybe because wasn't specifying namespace. if changed to

public icollection<mylib.client> fetch<mylib.client>() 

then got error:

type parameter declaration must identifier not type

on fetch<mylib.client>()

and if changed to:

public icollection<mylib.client> fetch<client>() 

then got error:

'mylib.clientlist' not implement interface member 'mylib.idatarequest.fetch()'. 'mylib.clientlist.fetch()' cannot implement 'mylib.idatarequest.fetch()' because not have matching return type of 'system.collections.generic.icollection'.

i don't have big knowledge of generics , have reached limit of can try cargo cult attempts. want possible? if can show me both interface method signature , example of class method definition.

as requested in comment here client class:

namespace mylib {     using system.data;     using system.runtime.serialization;      [datacontract]     public class client     {         public client(datarow clientrecord)         {             this.clientid = clientrecord.field<string>("id");             this.cphh = clientrecord.field<string>("id");             this.name = clientrecord.field<string>("name");             this.address = clientrecord.field<string>("address");              if (commonutilities.gis.validateosmapref(clientrecord.field<string>("locationd")))             {                 this.location = string.format(                     "{0}, {1}",                     commonutilities.gis.convertmapreftoeasting(clientrecord.field<string>("locationd")),                     commonutilities.gis.convertmapreftonorthing(clientrecord.field<string>("locationd")));             }         }          [datamember]         public string clientid { get; private set; }          [datamember]         public string name { get; private set; }          [datamember]         public string address { get; private set; }          [datamember]         public string location { get; private set; }          [datamember]         public string cphh { get; private set; }     } } 

you've defined interface incorrectly. try this:

public interface idatarequest<t> {     icollection<t> fetch(); } 

then classes this:

public class clientlist : idatarequest<client> {     public icollection<client> fetch()     {         //do stuff        return this.createcollection();     }      private icollection<client> createcollection()     {         list<client> clientlist = new list<client>();         // populate list         return clientlist;     } }  public class productlist : idatarequest<product> {     public icollection<product> fetch()     {         //do stuff        return this.createcollection();     }      private icollection<product> createcollection()     {         list<product> productlist = new list<product>();         // populate list         return productlist ;     } } 

that compiles nicely.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -