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.