Declare variable of a generic class

mosquito87

I have the following abstract class:

public abstract class ViewModel<TPrimaryModel> : ObservableObject
    where TPrimaryModel : TwoNames, new()
{
    ...
}

In another class I would like to declare a variable where a ViewModel could be saved to:

public class MainViewModel
{
    private ViewModel _currentViewModel;
}

This doesn't work as 2 parameters are required (because of the generic). I don't mind which ViewModel is saved to _currentViewModel, as long as the object inherits from the abstract class ViewModel.

This doesn't work as well:

public class MainViewModel
{
    #region Members
    private ViewModel<TwoNames> _currentViewModel;
    #endregion
}

Compiler error:

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

This is because TwoNames is an abstract class. Removing the "new()" constraint isn't a solution for me as I need to instantiate new objects of "TwoNames" in my abstract class (ViewModel). Any other idea?

Thomas Levesque

You need to create a non-generic base class:

public abstract class ViewModel : ObservableObject
{
    ...
}

public abstract class ViewModel<TPrimaryModel> : ViewModel
    where TPrimaryModel : TwoNames, new()
{
    ...
}

And declare _currentViewModel as type ViewModel:

public class MainViewModel
{
    #region Members
    private ViewModel _currentViewModel;
    #endregion
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declare generic variable type

From Dev

PHP Declare class with a variable

From Dev

Declare a variable class

From Dev

VHDL: How to declare a variable width generic

From Dev

Declare a static variable in an enum class

From Dev

is it valid to declare css variable in class?

From Dev

Declare and reuse variable in a React Class?

From Dev

Declare variable only with part of class

From Dev

In PowerShell 5 is it possible to declare generic properties for a class?

From Dev

How to declare uninitialized variable in abstract class in Java?

From Dev

is it possible to declare a Java Class variable using getClass()?

From Dev

How to force a class to declare a final variable?

From Dev

How to declare List variable in abstract class

From Dev

how to declare uninitialized variable in class definition in python

From Dev

How do declare global variable in PHP class

From Dev

How to declare a static variable with class.js?

From Dev

how to use variable which is declare in another class

From Dev

declare this property in your class or use a local variable

From Dev

How to declare variable of the type System.Collections.Generic.IEnumerable?

From Dev

Unable to invoke method declare in class implement generic interface method

From Dev

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

From Dev

Unable to invoke method declare in class implement generic interface method

From Dev

Generic inner class with instance variable that is an object of another generic inner class

From Dev

Why we declare static variable in a class & the definition in outside of the class?

From Dev

How to declare Class Instance Variable in inherited class automatically

From Dev

can i declare a class with generic form and then cast the subclasses to the type that is declared generic in java

From Dev

variable in generic class cannot be represented in objective c

From Dev

scala variable number of generic types in class

From Dev

Variable length type parameters in generic class definition

Related Related

  1. 1

    Declare generic variable type

  2. 2

    PHP Declare class with a variable

  3. 3

    Declare a variable class

  4. 4

    VHDL: How to declare a variable width generic

  5. 5

    Declare a static variable in an enum class

  6. 6

    is it valid to declare css variable in class?

  7. 7

    Declare and reuse variable in a React Class?

  8. 8

    Declare variable only with part of class

  9. 9

    In PowerShell 5 is it possible to declare generic properties for a class?

  10. 10

    How to declare uninitialized variable in abstract class in Java?

  11. 11

    is it possible to declare a Java Class variable using getClass()?

  12. 12

    How to force a class to declare a final variable?

  13. 13

    How to declare List variable in abstract class

  14. 14

    how to declare uninitialized variable in class definition in python

  15. 15

    How do declare global variable in PHP class

  16. 16

    How to declare a static variable with class.js?

  17. 17

    how to use variable which is declare in another class

  18. 18

    declare this property in your class or use a local variable

  19. 19

    How to declare variable of the type System.Collections.Generic.IEnumerable?

  20. 20

    Unable to invoke method declare in class implement generic interface method

  21. 21

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

  22. 22

    Unable to invoke method declare in class implement generic interface method

  23. 23

    Generic inner class with instance variable that is an object of another generic inner class

  24. 24

    Why we declare static variable in a class & the definition in outside of the class?

  25. 25

    How to declare Class Instance Variable in inherited class automatically

  26. 26

    can i declare a class with generic form and then cast the subclasses to the type that is declared generic in java

  27. 27

    variable in generic class cannot be represented in objective c

  28. 28

    scala variable number of generic types in class

  29. 29

    Variable length type parameters in generic class definition

HotTag

Archive