How Do I create a MCPP Property which returns an enum? - My Class will not compile, can you fix it?

R

Russell Mangel

I am trying to create a property which returns an enum.
The following class does not work, can someone fix it?
I am using VS2003 C++

public __gc class Drive
{
public:
Drive(){}
__property DriveType get_DriveType()
{
return _DriveType;
}
// This is the enum type
__value enum DriveType
{
CDROM, FIXED, REMOVABLE
};

private:
DriveType _DriveType;
};

Thanks
Russell Mangel
Las Vegas, NV
 
B

Ben Schwehn

Russell said:
I am trying to create a property which returns an enum.
The following class does not work, can someone fix it?
I am using VS2003 C++

Russell,

I believe the problem is caused by having a property with the same name
as the enum. (Both called DriveType)

The two possibilities I can see are:

1. rename the Enum (or the property)

public __gc class Drive
{
public:
Drive(){}
__property int get_DriveType()
{
return 2;// _DriveType;
}
__value enum DriveType2
{
CDROM, FIXED, REMOVABLE
};

private:
DriveType2 _DriveType;
};


2. Declare the enum outside the class and use the scope operator:

__value enum DriveType
{
CDROM, FIXED, REMOVABLE
};


public __gc class Drive
{
public:
Drive(){}
__property int get_DriveType()
{
return 2;// _DriveType;
}

private:
::DriveType _DriveType;
};


hth
 
T

Tomas Restrepo \(MVP\)

Hi Russell,
I am trying to create a property which returns an enum.
The following class does not work, can someone fix it?
I am using VS2003 C++

public __gc class Drive
{
public:
Drive(){}
__property DriveType get_DriveType()
{
return _DriveType;
}
// This is the enum type
__value enum DriveType
{
CDROM, FIXED, REMOVABLE
};

private:
DriveType _DriveType;
};

Thanks
Russell Mangel
Las Vegas, NV

There are two problems with your code:
The first one is that the enum is defined after the property, so you need to
move it above it so that it can be seen at that point by the compiler.
The second is that the property getter will end up generating a property
that has the same name as the type, so that Drive::DriveType will be
ambiguous (it would mean both the type and the property). This can be easily
fixed by renaming either one. Also, you'll want to make the enum definition
public, as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top