'TryCast' operands must be class-constrained type parameters, but '<typeparametername>' has no class constraint

The TryCast operator is used with a type parameter operand that is not guaranteed to be a reference type.

TryCast operates only on reference types, such as classes or interfaces. When you pass a type parameter as an argument to TryCast, you must constrain that type parameter to be a reference type. You can do this by including one or more of the following in the type parameter's constraint list:

  • One or more interface names (the type argument must implement all of them)

  • At most one class name (the type argument must inherit from it)

  • The New (Visual Basic) constraint (the type argument must expose a parameterless constructor that the creating code can access, and therefore it must be a class)

  • The Class (Visual Basic) constraint (the type argument must be a reference type)

Error ID: BC30793

To correct this error

  • If you need to pass this type parameter to TryCast, constrain it with one or more of the constraints in the preceding list.

  • If you cannot require the type parameter to accept only a reference type, you cannot use it with TryCast. You might be able to use the CType Function instead.

See Also

Concepts

Generic Types in Visual Basic

Value Types and Reference Types

Widening and Narrowing Conversions

Implicit and Explicit Conversions

Reference

Type List