Is it possible to use VB6 enums which have values with spaces in C#?

773 views Asked by At

We have a VB6 DLL which includes several enum definitions. We have loaded the VB6 DLL as a reference in a C# project.

In VB6, you can declare enum values which have names with spaces in them, using a special square bracket syntax. Examples:

Public Enum TestEnumNoSpaces
    EnumA
    EnumB
    EnumC
End Enum

Public Enum TestEnumWithSpaces
    [Enum A]
    [Enum B]
    [Enum C]
End Enum

If you compile this to a DLL and load it as a reference in Visual Studio 2010, in the object browser both enums are visible. However, the enum values are not shown for TestEnumWithSpaces, and also are not listed when using intellisense within C# code. The values for TestEnumNoSpaces are listed normally.

If I click TestEnumWithSpaces in a code window and select Go To Definition, I get something like this:

namespace DLLName
{
    [Guid("3DD0C797-2BF0-4A7A-8E1E-83E3095CB3AE")]
    public enum TestEnumWithSpaces
    {
    }
}

and similarly for TestEnumNoSpaces:

namespace DLLName
{
    [Guid("9A7152DB-20D7-49D8-8E33-E74F895DFE05")]
    public enum TestEnumNoSpaces
    {
        EnumA = 0,
        EnumB = 1,
        EnumC = 2,
    }
}

Which shows the same thing - enum exists with no values.

Is there something we can do to get the enum values to show up? In the question Spaces in C# Enums there is a method shown for enums which are defined within C#, but that does not apply to enums which are imported via COM interop as in my case.

Note - I could modify the VB6 DLL if necessary but this is not preferred since a bunch of other VB6 code would have to change accordingly.

Thanks

1

There are 1 answers

1
StayOnTarget On BEST ANSWER

Expanding on the suggestion by @hvd, I constructed this example in VB6:

Public Enum TestEnumWithSpaces
    [Enum A] = 0
    EnumA = [Enum A]
    [Enum B] = 1
    EnumB = [Enum B]
    [Enum C] = 2
    EnumC = [Enum C]
End Enum

This does work as intended - the enum values without spaces show up in C#.

I think this may be a viable choice, since we have the ability to modify the VB6 DLLs, though it is rather maintenance heavy at the outset. Over time, the idea would be to no longer add new enum values with spaces in them.

I haven't checked if this breaks binary compatibility, but that would be a downside if so, though we could live with it.