I tried to translate following C# code
public static class ObjectSetExtensions
{
public static void AddObjects<T>(this ObjectSet<T> objectSet, IEnumerable<T> objects)
{
foreach (var item in objects)
{
objectSet.AddObject(item);
}
}
}
to VB.NET:
Module ObjectSetExtensions
<System.Runtime.CompilerServices.Extension()>
Public Sub AddObjects(Of T)(ByVal objectSet As ObjectSet(Of T), ByVal objects As IEnumerable(Of T))
For Each item In objects
objectSet.AddObject(item)
Next
End Sub
End Module
But I getting an error that says:
Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'TEntity'.
What am I missing?
In C and C++ programming language terminology, a translation unit (or more casually a compilation unit) is the ultimate input to a C or C++ compiler from which an object file is generated.
A translation unit consists of an implementation file and all the headers that it includes directly or indirectly. Implementation files typically have a file extension of . cpp or . cxx .
A translator is a program that converts source code into object code. Generally, there are three types of translator: compilers. interpreters.
The C# version doesn't compile either, for the same reason. It should be:
public static void AddObjects<T>(this ObjectSet<T> objectSet,
IEnumerable<T> objects)
where T : class // Note this bit
{
foreach (var item in objects)
{
objectSet.AddObject(item);
}
}
And the VB version is:
<Extension> _
Public Sub AddObjects(Of T As Class)(ByVal objectSet As ObjectSet(Of T), _
ByVal objects As IEnumerable(Of T))
Dim local As T
For Each local In objects
objectSet.AddObject(local)
Next
End Sub
Note how in the VB version the constraint is part of the type parameter declaration. For more details, see MSDN.
It sounds like you are missing a constraint:
C#:
where T : class
VB:
(Of T as class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With