简体   繁体   中英

C#: Cannot create an instance of the abstract class or interface

i'm using a web service from HotelsPro and trying to create an instance but it gives me an error:

Cannot create an instance of the abstract class or interface 'HotelsPro.hotelsProSvc.b2bHotelSOAPPortType'

here is what i'm trying to do.

FORM1.CS

hotelsProSvc.b2bHotelSOAPPortType a = new b2bHotelSOAPPortType(); getAvailableHotelResponse getres = new getAvailableHotelResponse(); getres=a.getAvailableHotel(apiKey, "A990", checkIn, checkOut, "EUR", "UK", false, rooms, f);

In C# it is not possible to create an instance of an abstract class or interface.
b2bHotelSOAPPortType is either an interface or an abstract class. If you intend on creating a new object in this way (rather than using dependency injection for instance) then you must find a concrete class that implements b2bHotelSOAPPortType and new that.

For example

hotelsProSvc.b2bHotelSOAPPortType a = new Concreteb2bHotelSOAPPortType();

assuming Concreteb2bHotelSOAPPortType implements b2bHotelSOAPPortType.

A better approach would be to set up the relationship between these two classes in a DI container like Autofaq.

If b2bHotelSOAPPortType is an interface then it would be implemented thus

interface b2bHotelSOAPPortType {

}

if it is an abstract class then thus

abstract class b2bHotelSOAPPortType  {

}

What you need is to find a class that either implements b2bHotelSOAPPortType directly or indirectly. If it implements it directly it will look like this

class Concreteb2bHotelSOAPPortType : b2bHotelSOAPPortType {

}

If you are using Visual Studio you will be able to find which classes implement this interface or abstract class pretty easily.

Abstract classes or interfaces cannot be instanciated.

You'll have to instanciate a class that inherits from this class (or implements that interface).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM