3

I am currently porting some native code to C#, which consists of various structures, delegates, enumerations and external method calls.

Some of the fields within structures expect certain constants to be applied to them. It seems logical to list the constants within the structure, rather than in a separate location, as this should keep everything organized, but I am unsure what effect this would have on the structure during marshaling / interoperability with native calls.

Say for example my structure was defined as such:

[StructLayout(LayoutKind.Sequential)]
public struct NATIVE_STRUCTURE
{
     public int Value;
}

Value in this instance might require one of the following constants

VALUE1 = 0x0001;
VALUE2 = 0x0002;

So is it safe for me to write these structures like so:

[StructLayout(LayoutKind.Sequential)]
public struct NATIVE_STRUCTURE
{
     public int Value;

     public const int VALUE1 = 0x0001;
     public const int VALUE2 = 0x0002;
}

Can anyone shed some light onto how this might affect the code at runtime (if at all). Thanks.

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

2

It's better to use enums. Something like this:

[StructLayout(LayoutKind.Sequential)]
public struct NATIVE_STRUCTURE
{
    public NativeFoo FooValue;
}

public enum NativeFoo
{
   VALUE1 = 0x0001,
   VALUE2 = 0x0002,
}
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • Yes, I thought about doing it this way. Unfortunately this is going to add a hell of a lot of enumerations as there are about 2000 structures, and any one of them may require multiple enumerations. However +1 for effort, thanks! – Matthew Layton Oct 10 '12 at 10:52
  • 3
    @activwerx With that many structs, you might benefit from figuring out how to make [SWIG](http://www.swig.org/) do it for you. I personally haven't used it though. – Roman Starkov Oct 10 '12 at 10:54
  • VALUE1 and VALUE2 both have value 0x1. – Henrik Oct 10 '12 at 11:04
  • @Henrik, BD...I'm sure we can all still appreciate romkyns argument here! – Matthew Layton Oct 10 '12 at 12:22
  • 1
    @Henrik fixed - although I encourage you to just edit yourself whenever you spot an obvious error like that. – Roman Starkov Oct 10 '12 at 13:05
  • @activwerx I'm using always enums, even when this requires that some are duplicated. This makes writing code much(!!) clearer at the end. My current task is writting a new OpenGL-wrapper and OpenGL has a unbelivable amount of constants! – Felix K. Oct 10 '12 at 13:10
  • @romkyns, I did try to edit myself, however SO said I didn't change enough characters. But thanks all the same! – Matthew Layton Oct 10 '12 at 13:52
  • 1
    @FelixK, OK I may have to use your approach and find a naming convention for the enums that makes sense. – Matthew Layton Oct 10 '12 at 13:53
  • @activwerx This might be the hardest part, sometimes i had to put parts of the method name into the enum's name because some methods are basically the same but expecting other parameters. ( OpenGL: glTexImage2D and glTexSubImage2D ). – Felix K. Oct 10 '12 at 13:59
  • @FelixK. I was thinking of using the struct name, followed by the variable that is associated with the enum. I.E. my struct is called CryptMessage, and the variable is called dwFlag, therefore my enum is called CryptMessageFlag. Is there anything noticeably wrong with this implementation? – Matthew Layton Oct 10 '12 at 14:03
  • @activwerx No i don't think so, could result in long namens but you cannot prevent this anyway. – Felix K. Oct 10 '12 at 14:08