2

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

Community
  • 1
  • 1
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • 2
    "Is there something we can do to get the enum values to show up?" No - C# does not support spaces in Enum names. This is an example of a VB language feature that is not CLS-compliant (c# has them too, like public members that differ only by casing). If you plan do develop in multiple .NET languages consider adding the [`CLSCompliant`](https://msdn.microsoft.com/en-us/library/system.clscompliantattribute(v=vs.110).aspx) attribute to make sure you don't discover any more of these. – D Stanley Jun 12 '15 at 13:18
  • "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." -- You may be able to provide two separate names for the same enumeration value, so that the same enumeration type contains both `EnumA` and `Enum A`. This would avoid the need to update the other code using the VB6 DLL. –  Jun 12 '15 at 13:23
  • 1
    @DStanley but this DLL is a COM import, not a .NET project. So I would have thought the interop features could have taken care of this, and was hoping there might be a way to get that to happen. – StayOnTarget Jun 12 '15 at 14:09
  • 1
    This is not a duplicate of http://stackoverflow.com/questions/1117542/spaces-in-c-sharp-enums. That question deals with enums defined within C#. This question is about enums defined in a COM DLL, generated from VB6. – StayOnTarget Jun 12 '15 at 14:11
  • @hvd if that is the only solution it is a possible one, thanks. You might want to add that as an answer? – StayOnTarget Jun 12 '15 at 14:14
  • @DaveInCaz My apologies - I totally glossed over the fact that you are talking about VB6. – D Stanley Jun 12 '15 at 14:34
  • 1
    @DaveInCaz It's been too long since I used VB6 for me to know how to do that. If you can get that working, feel free to post it as an answer yourself. You should be able to more easily include enough details to help other people with the same problem. –  Jun 12 '15 at 14:38
  • You might get away with defining a typelib with spaces, once compiled and registered. – bugmagnet Oct 04 '15 at 15:28
  • Why was this question downvoted? – StayOnTarget Apr 24 '16 at 14:28

1 Answers1

0

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • Though this works, I'm going to wait to mark it as the answer, just in case there is a clever solution at the COM / referencing level. – StayOnTarget Mar 30 '16 at 10:57