Featured post
c# - Converting from an interface to an object that implements the interface? -
i have interface implemented several different objects. trying write method in c# accept interface object parameter , convert parameter object implements don't have write same function several different types. such as:
class unappliedcashdetails implements interface itransactiondetail.
constructor issbatch:
public issbatch(list<itransactiondetail>
details) public static issbatch getnextreceiptbatch() { list<unappliedcashdetail>
details = new list<unappliedcashdetail>
();
/`*`some code here populate list`*`/ return = new issbatch(details);
}
c# not this. trying use interface wrong or not casting correctly?
thanks!
you're passing list<unappliedcashdetail>
constructor accepts list<itransactiondetail>
. unappliedcashdetail
may implement itransactiondetail
, type of variance not supported c#. consider inside constructor (or other method) attempt add instance of someothertransactiondetail
details list, except details list should really accept unappliedcashdetail
, per declaration.
to make code work, need change declaration
list<itransactiondetail> details = new list<itransactiondetail>(); /* code here populate list */ return new issbatch(details);
or change constructor accept ienumerable<itransactiondetail>
, in case original list<unappliedcashdetail>
declaration work. variance supported ienumerable<t>
(note: c# 4), since sequence , cannot added to, deleted from, etc., there's no possibility of trying add appliedcashdetail
instance sequence of unappliedcashdetail
objects.
- Get link
- X
- Other Apps
Comments
Post a Comment