Example generic class
Class Container<T> Method Add(item As T) Dim temp As T End Method End Class
You can define and use generic classes, structures, traits, and procedures. Be aware that the ViviFire standard libraries provide many commonly used generic elements that you can use in your code.
Even though procedures are not types, you can define and use generic procedures. See Generic Procedures for details.
Advantages of generic types
Example of a constraint
Types of constraints
There are five types of constraints.
- The type argument must be one in a particular branch of inheritance.
- The type argument must be either a reference type or a value type.
- The type argument must implement one or more traits.
- The type argument must implement a parameterless constructor.
- Two or more of the above in the shown sequence.
If you need to specify more than one requirement, you can put a comma between each one. Alternatively, you can use multiple Where clauses, each one on its own line.
- To require a sub-type, use the less-tan or less-than-or-equal operators ("<" or "<=").
- To require a reference type, use "Is Class".
- To require a value type, use "Is Struct".
- To require a trait, use "Does".
- To require a constructor, use "Constructor()". The parentheses are required.
See Where Clause (Generics) for more information.
Example of multiple constraints
Class MyClass<T> Where T Is Class Does Comparable Constructor() ' Class members go here End Class
Class MyClass<T> Where T Does Comparable, T Is Class, T Constructor() ' Class members go here End Class
Class MyClass<T> Where T Does Comparable Where T Is Class Where T Constructor() ' Class members go here End Class