views:

164

answers:

2

Hello! Please take a look at the following code:

Public Sub Method(Of TEntity As EntityObject)(ByVal entity As TEntity)
    If TypeOf entity Is Contact Then  
        Dim contact As Contact = entity 'Compile error'
        Dim contact = DirectCast(entity, Contact) 'Compile error' 
        Dim contact = CType(entity, Contact) 'Compile error'
    End If
End Sub

Any ideas?

A: 

This should not give you a compile error and should properly cast it to the type you are looking for:

Dim contact As Contact = TryCast(entity, Contact)
Joseph
You are wrong my friend, the compile error is thrown by the DirectCast function.
Shimmy
In VB9, your code is equivalent to his code.
Meta-Knight
@Meta you're right, I've edited.
Joseph
Why won't you try your answers before you post them?
Shimmy
A: 

Either one of the following works, I guess I will use the first one:

Dim contact As Contact = DirectCast(entity, Object) 
Dim contact As Contact = Convert.ChangeType(entity, TypeCode.Object)
Shimmy