0

I'm trying to pass a child object in a Base class defined parameter but it doesn't work. I think I'm doing something wrong but I can't seem to figure out what.

This is the method I'm trying to execute:

 public static Tuple<List<AutoTaskBriljant>, List<TransactionResult>> MakeXMLFromObjectList(List<AutoTaskBriljant> lstObjects, 
        Func<BriljantObjects, IEnumerable<TransactionResult>> CheckObjectInput, string name)

The base class I was talking about is AutoTaskBriljant

I'm trying to execute the method here:

public static Tuple<List<Herstelling>, List<TransactionResult>> MakeXMLFromHerstellingenList(List<HerstellingTicket> lstTickHerst, string fileNameStart)
    {            
        return TransactionHelper.MakeXMLFromObjectList(lstTickHerst, CheckHerstellingInput, fileNameStart);
    }

The class HerstellingTicket is a child of AutoTaskBriljant

public class HerstellingTicket:AutoTaskBriljant
{
    public HerstellingTicket(Ticket atObj, Herstelling brObj) : base(atObj, brObj)
    {
    }
}

Thanks in advance

  • 1
    And "it doesn't work" means what exactly? Does it compile? – Steffen Harbich Aug 31 '16 at 13:55
  • 1
    ``List`` is never equal to ``List`` – Ehsan Sajjad Aug 31 '16 at 13:55
  • The code doesn't compile: I get the following error output: Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List – Jasper Callens Aug 31 '16 at 13:57
  • Even this won't work: List lstATBR = new List(); - Same error – Jasper Callens Aug 31 '16 at 13:59
  • 1
    They're not the same thing, so why would you expect that to work? If you had a `List` that you were allowed to cast to `List`, you'd then be allowed to add an `Orange` to your `List`... which is clearly wrong. – Charles Mager Aug 31 '16 at 14:02
  • Possible duplicate of [C# variance problem: Assigning List as List](http://stackoverflow.com/questions/2033912/c-sharp-variance-problem-assigning-listderived-as-listbase) – Charles Mager Aug 31 '16 at 14:04
  • @CharlesMager: List lstFruit = List lstApples should work right? I thought that was what I'm doing here – Jasper Callens Aug 31 '16 at 14:07
  • @JasperCallens no, that's exactly what I just described. You'd be allowed to add `Orange` to `lstFruit`, which can't work as it's actually a `List`. `List` is not covariant. I suggest reading about [covariance and contravariance in generics](https://msdn.microsoft.com/en-gb/library/dd799517%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). – Charles Mager Aug 31 '16 at 14:09
  • Oh okay I get it now! Thank you @CharlesMager – Jasper Callens Aug 31 '16 at 14:12

0 Answers0