A simple way of doing it is: [ StructLayout( LayoutKind.Sequential ) ] public struct MyStruct { public Initialize( ) // can't have a def. ctor... :( { b = new ushort[ 4 ]; c = new ushort[ 10 ]; }
public ushort a; [ MarshalAs( UnmanagedType.ByValArray , SizeConst = 4 , ArraySubType = UnmanagedType.U2 ) ] public ushort[] b; [ MarshalAs( UnmanagedType.ByValArray , SizeConst = 10 , ArraySubType = UnmanagedType.U2 ) ] public ushort[] c; } hope it helps... jmn On Thu, 6 Jun 2002 11:21:20 +0200, Pierre <[EMAIL PROTECTED]> wrote: >Hi, >I have a structure than contains two arrays with the same value type but >different lenght. Looking on past messages I got some interesting tips. The >C++ structure is: > >struct MyStruct >{ > ushort a; > ushort[4] b; > ushort[10] c; >} > >In C# I translated as: > >[StructLayout(LayoutKind.Sequential)] >struct MyStruct >{ > ushort a; > public ushort_vector_4 b; > public ushort_vector_10 c; > > [StructLayout(LayoutKind.Explicit)] > public struct ushort_vector_4 > { > public const int DataLength = 4; > [FieldOffset(0)]private ushort Header; > [FieldOffset((DataLength - 1) * 2)]private ushort Footer; > public int Length { get { return DataLength; } } > public unsafe ushort this[int index] > { > get > { > if ( (index < 0) || (index >= DataLength) ) > { > throw new IndexOutOfRangeException(); > } > fixed (ushort *head = &Header) > return *(head+index); > } > set > { > if ( (index < 0) || (index >= DataLength) ) > { > throw new IndexOutOfRangeException(); > } > fixed (ushort *head = &Header) > *(head +index) = value; > } > } > } > > [StructLayout(LayoutKind.Explicit)] > public struct ushort_vector_10 > { > public const int DataLength = 10; > [FieldOffset(0)]private ushort Header; > [FieldOffset((DataLength - 1) * 2)]private ushort Footer; > public int Length { get { return DataLength; } } > public unsafe ushort this[int index] > { > get > { > if ( (index < 0) || (index >= DataLength) ) > { > throw new IndexOutOfRangeException(); > } > fixed (ushort *head = &Header) > return *(head+index); > } > set > { > if ( (index < 0) || (index >= DataLength) ) > { > throw new IndexOutOfRangeException(); > } > fixed (ushort *head = &Header) > *(head +index) = value; > } > } > } >} > >There is an high level of redundancy of the code. Is it possibile to use >only one internal struct defining the size externally ? > >Thank you >Pierre > >You can read messages from the DOTNET archive, unsubscribe from DOTNET, or >subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.