Simon
Simon

Reputation: 2035

Create object with generic class as list

I have this situation:

public class ExtResult<T>
{
    public bool Success { get; set; }
    public string Msg { get; set; }
    public int Total { get; set; }
    public T Data { get; set; }
}

//create list object:
List<ProductPreview> gridLines;
...
...
//At the end i would like to create object
ExtResult<gridLines> result = new ExtResult<gridLines>() { 
    Success = true, Msg = "", 
    Total=0, 
    Data = gridLines 
}

But I get an error:

error: "cannot resolve gridLines"

What can I do to fix this?

Upvotes: 0

Views: 49

Answers (2)

Lee
Lee

Reputation: 144136

gridLines is a variable, its type is List<ProductPreview> which you should use as the type parameter to ExtResult<T>:

ExtResult<List<ProductPreview>> result = new ExtResult<List<ProductPreview>>() { 
    Success = true, 
    Msg = "", 
    Total=0, 
    Data = gridLines 
};

Upvotes: 4

awesoon
awesoon

Reputation: 33671

You should pass a type as a generic argument, not a variable:

var result = new ExtResult<List<ProductPreview>> // not gridLines, but it's type
{ 
    Success = true,
    Msg = "",
    Total=0,
    Data = gridLines
}

Upvotes: 2

Related Questions