rabejens
rabejens

Reputation: 8142

How to implement an interface defined in VB.net in VC++/CLI?

I have a VB.net based interface like this:

Namespace Foo

    Public Interface Bar 

        ReadOnly Property Quuxes as Quux()
    End Interface
End Namespace

I now want to implement this in VC++/CLI (because I need to interface functions from an unmanaged third-party DLL), however I cannot figure out the correct syntax how to implement it.

Here is the relevant part of my header file I have so far:

namespace Foo {

    public ref class ThirdPartyInterfacingBar : Bar {
    public:
        ThirdPartyInterfacingBar();

        virtual property array<Quux^, 1>^ Quuxes;
    };
}

but now I am stuck on how to implement this in the accompanying .cpp file.

When doing something like (#include stripped)

namespace Foo{

    array<Quux^, 1>^ ThirdPartyInterfacingBar::Quuxes { /*...*/ }
}

I get: C2048: function 'cli::array<Type,dimension> ^Foo::ThirdPartyInterfacingBar::Quuxes::get(void)' already has a body

The only thing I can think of is something like this:

namespace Foo {

    public ref class ThirdPartyInterfacingBar : Bar {
    private:
        array<Quux^, 1>^ delegateGetQuuxes();
    public:
        ThirdPartyInterfacingBar();

        virtual property array<Quux^, 1>^ Quuxes {
            array<Quux^, 1>^ get() {
                return delegateGetQuuxes();
            }
        }
    };
}

and implementing delegateGetQuuxes in the accompanying cpp file. But I think this is ugly because I do not want to have any logic in headers. Is there a better way?

Upvotes: 0

Views: 75

Answers (1)

Hans Passant
Hans Passant

Reputation: 942000

Looks like you just forgot get(). Proper syntax is:

.h file:
public ref class ThirdPartyInterfacingBar : Bar {
public:
    property array<Quux^>^ Quuxes {
        virtual array<Quux^>^ get();
    }
};

.cpp file:
array<Quux^>^ ThirdPartyInterfacingBar::Quuxes::get() {
    return delegateGetQuuxes();
}

Upvotes: 1

Related Questions