In one way or another, u'll always need to "construct" the structure,
so the ctor with a dummy parameter doesn't seams too bad.... :/
On the other hand, once b and c are ushort[], its bounds are implicitly
checked.... If u need to ensure that b and c aren't changed (e.g.
mystruct.b = null;), the u can make them private and add a new property
like:
public ushort[] B{ get{ return b; } };
In fact, to use this code u only need to:
MyStruct s = new Mystruct( 0 /* dummy parameter - assume it to be int */);
s.B[ 2 ] = 10; // will assign 10 to b[2]!
s.B[ 4 ] = 10; // will throw an IndexOutOfRangeException!!!!
calling the C function foo( MyStruct * ), will be as ez as: foo( s );
jmn
On Fri, 7 Jun 2002 13:00:25 +0200, Pierre Greborio
<[EMAIL PROTECTED]> wrote:
>OK, but I need to call Initialize method first. Then I could create a
>new constructor passing an unused parameter and then call it. I would do
>it without calling any constructor or method.
>
>Pierre
>
>-----------------------------------------------
>Pierre Greborio
>[EMAIL PROTECTED]
>http://www.pierregreborio.it
>-----------------------------------------------
>
>
>
>-----Original Message-----
>From: The DOTNET list will be retired 7/1/02
>[mailto:[EMAIL PROTECTED]] On Behalf Of Jose M. Nobre
>Sent: Friday, June 07, 2002 10:55 AM
>To: [EMAIL PROTECTED]
>Subject: Re: [DOTNET] Array on struct
>
>
>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.
>
>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.