Aracılığıyla paylaş


Type parameter '<typeparametername1>' must have either a 'New' constraint or a 'Structure' constraint to satisfy the 'New' constraint for type parameter '<typeparametername2>'

A statement constructs a generic type passing a type parameter that is not constrained to satisfy a New constraint.

The New constraint means that the type argument supplied to that type parameter must expose a parameterless constructor accessible to the code that creates objects from it. All value types have parameterless constructors, but not all reference types do. Therefore the Structure constraint satisfies the New constraint, but the Class constraint, or a class or interface name, does not.

The following statements can generate this error.

Public Class c1(Of t As New)
End Class
Public Class c2(Of u)
    Public testObject As New c1(Of u)
End Class

When class c2 creates an object from c1, type parameter u does not satisfy the New constraint on type parameter t.

Error ID: BC32084

To correct this error

  • If the type parameter to be passed to the generic type can satisfy the Structure or New constraint, then add the appropriate constraint to its definition.

    Public Class c2(Of u As Structure)
    
  • If the type parameter cannot satisfy the Structure or New constraint, then do not pass it to the generic type. You must pass something else as the type argument.

See Also

Concepts

Generic Types in Visual Basic

Value Types and Reference Types

Reference

New (Visual Basic)

Structure (Visual Basic)

Class (Visual Basic)