DataPortal.Delete = Instances of abstract classes cannot be created

DataPortal.Delete = Instances of abstract classes cannot be created

Old forum URL: forums.lhotka.net/forums/t/5832.aspx


ScottFortner posted on Wednesday, November 19, 2008

I'm getting this odd error message, and not sure how to handle.  I've got the following class:

<Serializable()> _
Public MustInherit Class RequirementBase
   Inherits BusinessBase(Of RequirementBase)
...
Public Shared Sub DeleteRequirement(ByVal Id As System.Int32)
 
If Not CanDeleteRequirement() Then
    Throw New System.Security.SecurityException("User not authorized to delete Requirement")
  End If
  DataPortal.Delete(New Criteria(Id))
End Sub
...
<Transactional(TransactionalTypes.TransactionScope)> _
Private Overloads Sub DataPortal_Delete(ByVal p_oCriteria As Criteria)
  DL_Requirement.DeleteById(p_oCriteria.Id)
End Sub

In code, when trying to call the following (Requirement inherits RequirementBase):

Requirement.DeleteRequirement(2)

I get the following asp.net message:

Instances of abstract classes cannot be created.

on DataPortal.Delete(New Criteria(Id)) in the DeleteRequirement Shared Sub

Any ideas on why?  I'm just trying to delete an object from the db, without loading the object first.  Thanks

RockfordLhotka replied on Wednesday, November 19, 2008

Notice that DataPortal_Delete() is an instance method, so the data portal must create an instance of your class before it can invoke that method. Since your class is abstract, it fails to create the instance.

You need to either change the class to not be abstract, or change your Criteria class to specify a non-abstract subclass that can be created.

ScottFortner replied on Monday, November 24, 2008

I've changed my DataPortal call to:

 

Public Shared Sub DeleteCode(ByVal p_iId As Int64)

   DataPortal.Delete(New SingleCriteria(Of Code, Int64)(p_iId))

 

However,  now I get:

"Invalid operation - delete not allowed"

Is that for the same reason?  Because it is using abstract classes? Thanks.

RockfordLhotka replied on Monday, November 24, 2008

That exception indicates that you don’t have a correct overload of the method for the data portal to call.

 

The data portal will attempt to invoke

 

Sub DataPortal_Delete(ByVal criteria As SingleCriteria(Of Code, Int64))

 

If it can’t find one of those, it falls back to

 

Sub DataPortal_Delete(ByVal criteria As Object)

 

And BusinessBase provides a default of this second overload that throws an invalid operation exception. You must either implement a strongly-typed match for the method, or override the one that takes an Object parameter.

 

Rocky

 

JoeFallon1 replied on Thursday, November 20, 2008

I think you have your inheritance set up incorrectly.

I'm getting this odd error message, and not sure how to handle.  I've got the following class:

<Serializable()> _
Public MustInherit Class RequirementBase
   Inherits BusinessBase(Of RequirementBase)
...
Public Shared Sub DeleteRequirement(ByVal Id As System.Int32)

In code, when trying to call the following (Requirement inherits RequirementBase):

You should declare it like this: (note the use of the final Type in the Inherits.)

<Serializable()> _
Public MustInherit Class RequirementBase
   Inherits BusinessBase(Of Requirement)

Joe

Copyright (c) Marimer LLC