How to use generics

Krowi

So, I have a class:

internal class GridBox<T> : BoxBase where T : new()
{
    public GridBox(Grid grid, GridBoxView view, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button)
        : base(grid, icon, caption, button)
    {
        View = view;
        DataSource = dataSource;
    }

    public GridBoxView View { get; set; }
    public ObservableCollection<T> DataSource { get; set; }
}

I use this GridBox Class here:

public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button) where T : IComparable<T>
    {
        Window window = Window.GetWindow(sender);
        Grid grid = Extensions.FindChild<Grid>(window);
        GridBoxView gridBox = new GridBoxView();

        return gridBox.Show<T>(new GridBox<T>(grid, gridBox, icon, caption, dataSource, button));
    }

I get an error here tho at the new GridBox<T>:

'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method

So, how can I use new GridBox<T> if the T is coming from public static T Show<T>?

Chris Sinclair

GridBox<T> has a constraint on T that requires the type to have a public parameterless constructor. This is what the where T : new() specifies. (See the MSDN article on the new constraint)

As such, when you attempt to use it in your Show method, the T there must still satisfy this constraint. If you update your Show method to:

public static T Show<T>(DependencyObject sender, MessageBoxIcon icon, string caption, ObservableCollection<T> dataSource, MessageBoxButton button)
    where T : IComparable<T>, new()

By adding the new() constraint to Show, you will satisfy the constraints for GridBox as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use dynamic dispatch in Rust for trait with generics?

From Java

How to use TypeToken + generics with Gson in Kotlin

From Java

How to use generics in props in React in a functional component?

From Dev

How do I use generics with HashMap?

From Dev

How to use generics in a java method

From Dev

How to use generics

From Dev

How to use the selName function of GHC.Generics?

From Dev

how to use generics in Scala

From Dev

How to use parentheses for generics?

From Dev

How to use generics for the similar methods with different classes

From Dev

How to use multiple bounds in java generics in this case

From Dev

how to use a class with generics without having to declare the generic type

From Dev

How to use generics with multiple coupled objects?

From Dev

How to use generics as params?(Swift 2.0)

From Dev

Swift how to use 'self' as generics

From Dev

How to use Jackson with nested Generics?

From Dev

How to use java generics with restTemplate

From Dev

How do I correctly use generics in this case?

From Dev

How to Use Generics in a map of Comparator to avoid warnings

From Dev

How to use Jackson with nested Generics?

From Dev

How to use generics in spring?

From Dev

How to use generics of generics in java interfaces

From Dev

how to use generics in Scala

From Dev

How to use generics in a method declaration

From Dev

How to use generics for the similar methods with different classes

From Dev

How to use generics to combine these two methods Into one?

From Dev

How to use generics in argument need class

From Dev

How to use Nim's `of` operator with generics?

From Dev

How to use generics properly for a Holder

Related Related

HotTag

Archive